Class: Lad::Bootstrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/lad.rb

Class Method Summary collapse

Class Method Details

.executeObject



6
7
8
9
10
11
12
13
14
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lad.rb', line 6

def self.execute

  # input_tokens = nil

  optparser = OptionParser.new do |opts|
    opts.banner = '''
  Usage: lad [-h|--help] [<path_to_git_repo> <project_name>]

  Templated project token replacer/bootstrapper
    '''
    opts.on('-h', '--help', 'Displays this screen') do |v|
      puts opts
      exit
    end

    # opts.on('-t TOKENS', '--tokens TOKENS', 'Tokens to replace in the form of, token:value token:value') do |tokens|
    #   input_tokens = Hash[*tokens.map { |v| 
    #     v.split ':'
    #   }.flat_map { |kvp| 
    #     ["__#{kvp.first.upcase}__", kvp.last] 
    #   }]
    # end
  end

  optparser.parse!

  files_processed = 0
  dirs_processed  = 0
  options         = Arguments.extract
  dir             = File.join(Dir.tmpdir, options[:name])
  config          = {}

  puts '' # insert new line padding

  Console.task 'Cloning git repository' do
    Files.delete_temporary_files(dir)
    Files.clone_and_orphan(options[:git_url], dir)
  end
  
  Console.task 'Loading configuration' do
    config = Config.load dir, {
      'token'  => '__NAME__'
    }
  end

  Console.task 'Parsing tokens and stuff' do
    puts ''
    config['token_values'] = Config.get_token_values config, options[:name]
    puts ''
  end

  Console.task 'Processing files' do
    Dir.glob(File.join dir, '**/*').each do |item|
      if !File.directory?(item)
        files_processed += 1  

        config['token_values'].each do |token|
          new_item = Files.new_filename item, *token

          if item != new_item
            File.rename(item, new_item)
            item = new_item
          end
          
          Files.replace_token_in_file config['ignore'], new_item, *token         
        end
      end
    end
  end

  Console.task 'Processing directories' do
    Dir.glob(File.join dir, '**/*').each do |item|
      if File.directory?(item)
        dirs_processed += 1
        config['token_values'].each do |token|
          new_item = Files.new_filename item, *token
          if item != new_item
            File.rename(item, new_item)
            item = new_item
          end   
        end
      end
    end
  end

  Console.task 'Moving project files' do
    File.rename dir, File.join(Dir.pwd, options[:name])
  end

  Console.success "\n  Done processing #{files_processed} file(s)"
  Console.success "  Done processing #{dirs_processed} directories(s)"
end