Module: Klipp

Defined in:
lib/klipp.rb,
lib/klipp/creator.rb,
lib/klipp/version.rb,
lib/klipp/configuration.rb,
lib/klipp/parameter_list.rb

Defined Under Namespace

Classes: ClusterError, Configuration, Creator, Hint, ParameterList

Constant Summary collapse

VERSION =
"0.2.2.4"

Class Method Summary collapse

Class Method Details

.cli_create(params, highline = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/klipp.rb', line 89

def self.cli_create(params, highline = nil)
  params = Klipp::ParameterList.new(params)
  if (interactive_identifier = params.shift_argument)
    creator = Klipp::Creator.from_user_input(interactive_identifier, highline)
    puts()
  else
    creator = Klipp::Creator.from_file File.join(Dir.pwd, 'Klippfile')
  end
  spec_path = Template::Spec.spec_path_for_identifier creator.identifier
  spec = Template::Spec.from_file spec_path

  validation_errors = Array.new

  begin
    spec.set_token_values(creator.tokens, params.splice_option('-v'))
  rescue Exception => e
    validation_errors << e.message
  end

  begin
    spec.confirm_required_files
  rescue Exception => e
    validation_errors << e.message
  end

  if validation_errors.length > 0
    e = Klipp::ClusterError.new
    e.messages = validation_errors
    raise e
  end

  block_actions = spec.block_actions_under_git && git_repository?
  if spec.pre_actions.count > 0
    if block_actions
      Formatador.display_line("[yellow][i][/] Git repository found, not running pre-actions (see .klippspec).")
      puts()
    else
      run_actions(spec.pre_actions) if Klipp.env.prod?
      Formatador.display_line("[green][√] Pre-actions complete.[/]")
      puts()
    end
  end

  force = params.splice_option('-f')

  source_dir = File.dirname(Template::Spec.spec_path_for_identifier creator.identifier)
  target_dir = Dir.pwd

  source_files = Dir.glob(File.join(source_dir, '**', '*'), File::FNM_DOTMATCH).reject { |f| f == spec_path }

  result = source_files.map do |source_file|
    spec.transfer_file source_file, spec.target_file(source_dir, source_file, target_dir), force
  end

  verbose = params.splice_option '-v'

  Formatador.display_line("[green][√] Creation completed using template #{Template::Spec.expand_identifier creator.identifier}. #{'Run `klipp create -v` to see what files were created.' unless verbose}[/]")
  puts()

  if (verbose)
    strip = File.dirname(Dir.pwd)+File::SEPARATOR
    result.each { |r| Formatador.display_line(r.gsub(strip, '')) unless File.directory? r }
    puts()
  end

  if spec.post_actions.count > 0
    if block_actions
      Formatador.display_line("[yellow][i][/] Git repository found, not running post-actions (see .klippspec).")
      puts()
    else
      run_actions(spec.post_actions) if Klipp.env.prod?
      Formatador.display_line("[green][√] Post-actions complete.[/]")
      puts()
    end
  end

  Formatador.display_line("[green][√] Done.[/]")
end

.cli_prepare(params = []) ⇒ Object

Raises:



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
# File 'lib/klipp.rb', line 63

def self.cli_prepare(params=[])
  params = Klipp::ParameterList.new(params)
  template = params.shift_argument
  raise Klipp::Hint.new("Add a template name to `klipp prepare [template]`. Use `klipp template list` to see your options.") unless template

  spec = Template::Spec.from_file Template::Spec.spec_path_for_identifier(template)
  filename = 'Klippfile'

  force = params.splice_option('-f')
  will_overwrite = File.exists?(filename) && force

  raise "#{filename} already exists, not overwriting. Use -f to force overwriting." if File.exists?(filename) && !force

  File.write('Klippfile', spec.klippfile)

  Formatador.display_line("[green][√] Prepared #{filename} #{'again' if will_overwrite}.[/]")

  capture_stdout {
    `open -a TextMate #{filename} 2>&1` if File.exists?(filename)
  }

  if $? && $?.exitstatus > 0
    `open -t #{filename}` if File.exists?(filename)
  end
end

.envObject



23
24
25
# File 'lib/klipp.rb', line 23

def self.env
  @@env ||= StringInquirer.new('prod')
end

.env=(env) ⇒ Object



27
28
29
# File 'lib/klipp.rb', line 27

def self.env=(env)
  @@env = env
end

.git_repository?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/klipp.rb', line 168

def self.git_repository?
  `git rev-parse --is-inside-work-tree 2>&1`.match /true/
end

.route(*argv) ⇒ Object



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
# File 'lib/klipp.rb', line 31

def self.route(*argv)
  params = Klipp::ParameterList.new(argv)
  command = params.shift_argument;
  commands = {
      prepare: lambda { cli_prepare(params) },
      create: lambda { cli_create(params) },
      template: lambda { Template.route(*params) }
  }
  case command
    when nil
      raise Klipp::Hint.new "Add a command to `klipp [#{commands.keys.join('|')}]`"
    else
      if commands[command.to_sym]
        commands[command.to_sym].call
      else
        raise "Unknown command `klipp #{command}`"
      end
  end
  0 # exit code
rescue Exception => e
  case e
    when Klipp::Hint
      Formatador.display_line("[yellow][?] #{e.message}[/]")
    when Klipp::ClusterError
      e.messages.each { |msg| Formatador.display_line("[red][!] #{msg}[/]\n") }
    else
      Formatador.display_line("[red][!] #{e.message}[/]\n")
      Formatador.display_line(e.backtrace.first)
  end
  1 # exit code
end

.run_actions(actions) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/klipp.rb', line 172

def self.run_actions(actions)
  count = actions.count()
  puts()
  actions.each do |action|
    Formatador.display_line("[yellow][i][/] Running `#{action}`...")
    puts()
    system(action) if Klipp.env.prod?
    puts()
    #IO.popen(action, :err=>[:child, :out]) { |f| puts '  '+f.read.gsub("\n", "\n  ") }
    raise "Error running action `#{action}`." if $? && $?.exitstatus > 0
  end
end