Sha256: d37a79d0e1127c2f05e629b81dae50f197398c21abeeb9e0a9165a539cd809be

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

module Hexlet
  class TeacherCLI < BaseCLI
    desc "submit PATH_TO_LESSON", "login on hexlet.io"
    def submit(path)
      lesson_folder = File.split(path)[1]
      parts = lesson_folder.split("_")

      if parts.last != "lesson"
        puts (t "wrong_lesson_folder")
        return false
      end

      slug = parts[0, parts.size - 2].join("_")
      locale = parts[-2]

      filepath = generate_lesson_tarball(path)

      result = client.submit slug, locale, filepath

      if result
        puts (t :created)
        true
      else
        puts (t :error)
        false
      end
    end

    private

    def generate_lesson_tarball(path)
      dist_path = File.join(path, "dist")
      exercise_tarball_path = File.join(dist_path, "exercise.tar.gz")

      FileUtils.mkdir_p(dist_path)

      File.open(exercise_tarball_path, 'wb') do |fd|
        sgz = Zlib::GzipWriter.new(fd)

        Archive::Tar::Minitar::Output.open(sgz) do |stream|
          FileUtils.cd path do
            files = Rake::FileList.new("**/*") do |f|
              f.exclude /node_modules/
              f.exclude /bower_components/
              f.exclude /dist/
              f.exclude ".git"
            end

            # raise files.inspect
            files.each do |entry|
              Archive::Tar::Minitar.pack_file(entry, stream)
            end
          end
        end
      end

      exercise_tarball_path
    end

    def client
      @client ||= Hexlet::TeacherClient.new config["hexlet_api_key"], logger: logger, host: options["host"]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hexlet-0.0.1 lib/hexlet/teacher_cli.rb