Module: Bauble::Cli::Commands::New

Included in:
BaubleCli
Defined in:
lib/bauble/cli/commands/new.rb

Overview

Up command

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bauble/cli/commands/new.rb', line 15

def included(thor) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  thor.class_eval do # rubocop:disable Metrics/BlockLength
    desc 'new [DESTINATION]', 'Scaffolds a new app from a GitHub repository'
    map 'new' => :new_app
    option :template, type: :string, default: 'basic-app',
                      desc: 'The template to use for the new app'

    def new_app(destination)
      github_repo = 'jamesh38/bauble'
      specific_dir = "examples/#{options[:template]}"
      dest_dir = destination ? "#{Dir.pwd}/#{destination}" : Dir.pwd
      begin
        uri = URI("https://api.github.com/repos/#{github_repo}/contents/#{specific_dir}")
        fetch_and_write_files(uri, dest_dir, specific_dir)
      rescue StandardError => e
        Logger.error e.message
        return
      end
      Logger.block_log 'New app created successfully'
    end

    no_commands do
      def fetch_and_write_files(uri, destination, specific_dir) # rubocop:disable Metrics
        response = Net::HTTP.get_response(uri)

        if response.is_a?(Net::HTTPSuccess)
          files = JSON.parse(response.body)

          files.each do |file|
            if file['type'] == 'file'
              next if file['name'] == 'Gemfile.lock'

              file_uri = URI(file['download_url'])
              file_content = Net::HTTP.get(file_uri)
              target_path = File.join(destination, file['path'].sub(specific_dir, ''))
              FileUtils.mkdir_p(File.dirname(target_path))
              File.write(target_path, file_content)
            elsif file['type'] == 'dir'
              new_uri = URI(file['url'])
              fetch_and_write_files(new_uri, destination, specific_dir)
            end
          end
        elsif response.is_a?(Net::HTTPNotFound)
          raise 'Failed to create new app: Unknown template'
        else
          raise "Failed to to create new app: #{response.message}"
        end
      end
    end
  end
end