falkorlib 0.3.14 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,230 @@
1
+ # -*- encoding: utf-8 -*-
2
+ ################################################################################
3
+ # Time-stamp: <Mar 2015-01-20 23:25 svarrette>
4
+ ################################################################################
5
+ # Interface for the CLI
6
+ #
7
+
8
+ require 'thor'
9
+ require 'thor/actions'
10
+ require "falkorlib"
11
+
12
+ require "falkorlib/cli/new"
13
+
14
+
15
+
16
+ module FalkorLib
17
+
18
+ # Falkor CLI Application, based on [Thor](http://whatisthor.com)
19
+ module CLI
20
+
21
+ # Main Application
22
+ class App < ::Thor
23
+ package_name "Falkor[Lib]"
24
+ map "-V" => "version"
25
+
26
+ namespace :falkor
27
+
28
+ include Thor::Actions
29
+ include FalkorLib::Common
30
+
31
+ #default_command :info
32
+
33
+ class_option :verbose, :aliases => "-v",
34
+ :type => :boolean, :desc => "Enable verbose output mode"
35
+ class_option :debug, :aliases => "-d",
36
+ :type => :boolean, :default => FalkorLib.config[:debug], :desc => "Enable debug output mode"
37
+ class_option :dry_run, :aliases => '-n', :type => :boolean
38
+
39
+ ###### commands ######
40
+ desc "commands", "Lists all available commands", :hide => true
41
+ def commands
42
+ puts App.all_commands.keys - ["commands", "completions"]
43
+ end
44
+
45
+ ###### config ######
46
+ desc "config", "Print the current configuration of FalkorLib", :hide => true
47
+ def config
48
+ info "Thor options:"
49
+ puts options.to_yaml
50
+ info "FalkorLib internal configuration:"
51
+ puts FalkorLib.config.to_yaml
52
+ end # config
53
+
54
+
55
+ # map %w[--help -h] => :help
56
+
57
+ ###### init ######
58
+ desc "new TYPE", "Initialize the directory PATH with FalkorLib's template(s)"
59
+ subcommand "new", FalkorLib::CLI::New
60
+
61
+
62
+
63
+ map %w[--version -V] => :version
64
+ ###### version ######
65
+ desc "--version, -V", "Print the version number"
66
+ def version
67
+ say "Falkor[Lib] version " + FalkorLib::VERSION, :yellow # + "on ruby " + `ruby --version`
68
+ end
69
+
70
+
71
+
72
+
73
+ end # class App
74
+ end # module CLI
75
+
76
+ end
77
+
78
+
79
+
80
+
81
+
82
+ # require_relative "commands/init"
83
+
84
+ # module FalkorLib
85
+ # module CLI
86
+ # def self.const_missing(c)
87
+ # Object.const_get(c)
88
+ # end
89
+ # end
90
+ # end
91
+
92
+ # require 'falkorlib'
93
+
94
+
95
+ # require 'thor'
96
+ # require "thor/group"
97
+
98
+
99
+ #require "falkorlib/commands/init"
100
+
101
+ # require 'logger'
102
+
103
+ # # Declare a logger to log messages:
104
+ # LOGGER = Logger.new(STDERR)
105
+ # LOGGER.level = Logger::INFO
106
+
107
+
108
+ # module FalkorLib
109
+ # # Falkor CLI Application, based on [Thor](http://whatisthor.com)
110
+
111
+ # require_relative "commands/init"
112
+
113
+ # module CLI
114
+
115
+ # def self.const_missing(c)
116
+ # Object.const_get(c)
117
+ # end
118
+
119
+
120
+ # # Main CLI command for FalkorLib
121
+ # class Command < Thor
122
+
123
+ # class_option :verbose, :type => :boolean
124
+
125
+ # require "falkorlib/commands/init"
126
+
127
+ # # ----------
128
+ # # desc "init <PATH> [options]", "Initialize the directory PATH with FalkorLib's template(s)"
129
+ # # subcommand "init", FalkorLib::CLI::Init
130
+
131
+ # end # class FalkorLib::CLI::Command
132
+ # end
133
+ # end
134
+
135
+
136
+
137
+
138
+ # # Mercenary version
139
+
140
+ # module FalkorLib
141
+
142
+ # # Falkor CLI Application, based on (finally) [Mercenary](http://www.rubydoc.info/gems/mercenary)
143
+ # # instead of [Thor](http://whatisthor.com)
144
+ # class Command
145
+
146
+ # # Keep a list of subclasses of FalkorLib::Command every time it's inherited
147
+ # # Called automatically.
148
+ # #
149
+ # # base - the subclass
150
+ # #
151
+ # # Returns nothing
152
+ # def self.inherited(base)
153
+ # subclasses << base
154
+ # end
155
+
156
+ # # A list of subclasses of FalkorLib::Command
157
+ # def self.subclasses
158
+ # @subclasses ||= []
159
+ # end
160
+
161
+ # def init_with_program(p)
162
+ # raise NotImplementedError.new("")
163
+ # end
164
+ # end
165
+ # end
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+ # module FalkorLib #:nodoc:
178
+ # module Config
179
+
180
+ # # Default configuration for FalkorLib::App
181
+ # module CLI
182
+ # # App defaults for FalkorLib
183
+ # DEFAULTS = {
184
+ # # command-line options
185
+ # :options => {},
186
+ # #:type => "latex"
187
+ # }
188
+ # end
189
+ # end
190
+ # end
191
+
192
+
193
+ # # ---------------------------
194
+ # # Command line parsing
195
+ # # ---------------------------
196
+ # module FalkorCLI
197
+ # class Init < Thor
198
+ # # # exit if bad parsing happen
199
+ # # def self.exit_on_failure?
200
+ # # true
201
+ # # end
202
+
203
+ # class_option :verbose,
204
+ # :type => :boolean,
205
+ # :default => false,
206
+ # :aliases => "-v",
207
+ # :desc => "Verbose mode"
208
+ # class_option :debug,
209
+ # :type => :boolean,
210
+ # :default => false,
211
+ # :aliases => "-d",
212
+ # :desc => "Debug mode"
213
+
214
+
215
+ # desc "init", "Initialise a given template"
216
+ # long_desc <<-LONGDESC
217
+ # Initialize a new directory according to <type> of template
218
+ # LONGDESC
219
+ # option :type,
220
+ # :required => true,
221
+ # :default => 'repo',
222
+ # :type => :string,
223
+ # :desc => "Type of template to use to initialize this repository"
224
+ # def init(type)
225
+ # LOGGER.level = Logger::DEBUG if options[:verbose]
226
+ # puts "Hello, world!"
227
+
228
+ # end
229
+ # end
230
+ # end # module FalkorLib
@@ -0,0 +1,94 @@
1
+ # -*- encoding: utf-8 -*-
2
+ ################################################################################
3
+ # Time-stamp: <Ven 2015-01-23 00:00 svarrette>
4
+ ################################################################################
5
+
6
+ require 'thor'
7
+ require 'falkorlib'
8
+ #require 'falkorlib/cli/init/repo'
9
+ require "falkorlib/bootstrap"
10
+
11
+ module FalkorLib
12
+ module CLI
13
+
14
+ # Thor class for all bootstrapping / initialization
15
+ class New < ::Thor
16
+
17
+ ###### repo ######
18
+ desc "repo NAME [options]", "Bootstrap a Git Repository"
19
+ long_desc <<-REPO_LONG_DESC
20
+ Initiate a Git repository according to my classical layout.
21
+ \x5 * the repository will be configured according to the guidelines of [Git Flow]
22
+ \x5 * the high-level operations will be piloted either by a Makefile (default) or a Rakefile
23
+
24
+ By default, NAME is '.' meaning that the repository will be initialized in the current directory.
25
+ \x5Otherwise, the NAME subdirectory will be created and bootstraped accordingly.
26
+ REPO_LONG_DESC
27
+ #......................................................
28
+ method_option :make, :default => true,
29
+ :type => :boolean, :desc => 'Use a Makefile to pilot the repository actions'
30
+ method_option :rake,
31
+ :type => :boolean, :desc => 'Use a Rakefile (and FalkorLib) to pilot the repository actions'
32
+ method_option :interactive, :aliases => '-i',
33
+ :type => :boolean, :desc => "Interactive mode"
34
+ method_option :remote_sync, :aliases => '-r',
35
+ :type => :boolean, :desc => "Operate a git remote synchronization with remote. By default, all commits stay local"
36
+ method_option :master,
37
+ :default => 'production', :banner => 'BRANCH', :desc => "Master Branch name for production releases"
38
+ method_option :develop, :aliases => [ '-b', '--branch', '--devel'],
39
+ :default => 'devel', :banner => 'BRANCH', :desc => "Branch name for development commits"
40
+ #___________________
41
+ def repo(name = '.')
42
+ # _newrepo(name, options)
43
+ FalkorLib::Bootstrap.repo(name, options)
44
+ end # repo
45
+
46
+
47
+ ###### trash ######
48
+ desc "trash PATH", "Add a Trash directory"
49
+ #________________________
50
+ def trash(path = Dir.pwd)
51
+ FalkorLib::Bootstrap.trash(path)
52
+ end # trash
53
+
54
+ ###### rvm ######
55
+ desc "rvm PATH [options]", "Initialize RVM"
56
+ long_desc <<-RVM_LONG_DESC
57
+ Initialize Ruby Version Manager (RVM) for the current directory (or at the root directory of the Git repository).
58
+ It consists of two files:
59
+ \x5 * `.ruby-version`: Project file hosting a single line for the ruby version
60
+ \x5 * `.ruby-gemset`: Gemset file hosting a single line for the gemset to use for this project
61
+
62
+ These files will be committed in Git to ensure a consistent environment for the project.
63
+ RVM_LONG_DESC
64
+ method_option :force, :aliases => '-f',
65
+ :type => :boolean, :desc => 'Force overwritting the RVM config'
66
+ method_option :ruby, :banner => 'VERSION',
67
+ :desc => 'Ruby version to configure / install for RVM'
68
+ method_option :versionfile, :banner => 'FILE',
69
+ :default => FalkorLib.config[:rvm][:versionfile], :desc => 'RVM ruby version file'
70
+ method_option :gemset, :desc => 'RVM gemset to configure for this directory'
71
+ method_option :gemsetfile, :banner => 'FILE',
72
+ :default => FalkorLib.config[:rvm][:gemsetfile], :desc => 'RVM gemset file'
73
+ #____________________
74
+ def rvm(path = '.')
75
+ FalkorLib::Bootstrap.rvm(path, options)
76
+ end # rvm
77
+
78
+ ###### versionfile ######
79
+ desc "versionfile PATH [options]", "initiate a VERSION file"
80
+ method_option :file, :aliases => '-f',
81
+ :desc => "Set the VERSION filename"
82
+ method_option :tag, :aliases => '-t',
83
+ :desc => "Git tag to use"
84
+ method_option :version, :aliases => '-v',
85
+ :desc => "Set the version to initialize in the version file"
86
+ #_______________
87
+ def versionfile(path = '.')
88
+ FalkorLib::Bootstrap.versionfile(path, options)
89
+ end # versionfile
90
+
91
+
92
+ end # class Init
93
+ end # module CLI
94
+ end # module FalkorLib
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  ################################################################################
3
- # Time-stamp: <Ven 2014-12-05 15:41 svarrette>
3
+ # Time-stamp: <Mer 2015-01-21 22:36 svarrette>
4
4
  ################################################################################
5
5
 
6
6
  require "falkorlib"
@@ -8,6 +8,7 @@ require 'open3'
8
8
  require 'erb' # required for module generation
9
9
  require 'diffy'
10
10
  require 'json'
11
+ require "pathname"
11
12
 
12
13
  module FalkorLib #:nodoc:
13
14
 
@@ -406,6 +407,24 @@ module FalkorLib #:nodoc:
406
407
 
407
408
  end
408
409
 
410
+ ###### normalize_path ######
411
+ # Normalize a path and return the absolute path foreseen
412
+ # Ex: '.' return Dir.pwd
413
+ # Supported options:
414
+ # * :relative [boolean] return relative path to the root dir
415
+ ##
416
+ def normalized_path(dir = Dir.pwd, options = {})
417
+ rootdir = FalkorLib::Git.init?(dir) ? FalkorLib::Git.rootdir(dir) : dir
418
+ path = dir
419
+ path = Dir.pwd if dir == '.'
420
+ path = File.join(Dir.pwd, dir) unless (dir =~ /^\// or dir == '.')
421
+ if (options[:relative] or options[:relative_to])
422
+ root = options[:relative_to] ? options[:relative_to] : rootdir
423
+ relative_path_to_root = Pathname.new( File.realpath(path) ).relative_path_from Pathname.new(root)
424
+ path = relative_path_to_root.to_s
425
+ end
426
+ return path
427
+ end # normalize_path
409
428
 
410
429
  end
411
430
  end
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  ################################################################################
3
- # Time-stamp: <Lun 2014-09-01 09:37 svarrette>
3
+ # Time-stamp: <Mer 2015-01-21 09:25 svarrette>
4
4
  ################################################################################
5
5
  # FalkorLib Configuration
6
6
  #
@@ -44,10 +44,13 @@ module FalkorLib #:nodoc:
44
44
  :root => Dir.pwd,
45
45
  :custom_cfg => '.falkorlib.yaml',
46
46
  :rvm => {
47
- :rubies => [ '1.9.3', '2.0.0', '2.1.0']
47
+ :rubies => [ '1.9.3', '2.0.0', '2.1.0'],
48
+ :versionfile => '.ruby-version',
49
+ :gemsetfile => '.ruby-gemset'
48
50
  },
49
51
  :templates => {
50
- :puppet => {}
52
+ :trashdir => '.Trash',
53
+ :puppet => {}
51
54
  },
52
55
  :tokens => {
53
56
  :code_climate => ''
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  ################################################################################
3
- # Time-stamp: <Ven 2014-12-05 22:43 svarrette>
3
+ # Time-stamp: <Jeu 2015-01-22 17:57 svarrette>
4
4
  ################################################################################
5
5
  # Interface for the main Git operations
6
6
  #
@@ -82,8 +82,10 @@ module FalkorLib #:nodoc:
82
82
  subl.any? { |i| l[i].split.include?(cmd) }
83
83
  end
84
84
 
85
- ## Initialize a git repository
86
- def init(path = Dir.pwd)
85
+ ###
86
+ # Initialize a git repository
87
+ ##
88
+ def init(path = Dir.pwd, options = {})
87
89
  # FIXME for travis test: ensure the global git configurations
88
90
  # 'user.email' and 'user.name' are set
89
91
  [ 'user.name', 'user.email' ].each do |userconf|
@@ -100,9 +102,9 @@ module FalkorLib #:nodoc:
100
102
  end
101
103
  end
102
104
  exit_status = 1
103
- #info "Initialize "
104
- Dir.chdir( path ) do
105
- execute "git init" unless FalkorLib.config.debug
105
+ Dir.mkdir( path ) unless Dir.exist?( path )
106
+ Dir.chdir( path ) do
107
+ execute "git init" unless FalkorLib.config.debug
106
108
  exit_status = $?.to_i
107
109
  end
108
110
  # #puts "#init #{path}"
@@ -139,6 +141,30 @@ module FalkorLib #:nodoc:
139
141
  g.branch (opts[:force] ? :D : :d) => "#{branch}"
140
142
  end
141
143
 
144
+ ###### config ######
145
+ # Retrieve the Git configuration
146
+ # You can propose a pattern as key
147
+ # Supported options:
148
+ # * :list [boolean] list all configutations
149
+ # * :hash [boolean] return a Hash
150
+ ##
151
+ def config(key, dir = Dir.pwd, options = {})
152
+ #info "Retrieve the Git configuration"
153
+ res = nil
154
+ if (options[:list] or (key.is_a? Regexp) or (key =~ /\*/))
155
+ cg = MiniGit::Capturing.new(dir)
156
+ res = (cg.config :list => true).split("\n")
157
+ res.select! { |e| e.match(key) } unless key =='*'
158
+ res = res.map { |e| e.split('=') }.to_h if options[:hash]
159
+ else
160
+ g = MiniGit.new(dir)
161
+ res = g[key]
162
+ end
163
+ #ap res
164
+ res
165
+ end
166
+
167
+
142
168
  ## Fetch the latest changes
143
169
  def fetch(path = Dir.pwd)
144
170
  Dir.chdir( path ) do
@@ -204,22 +230,25 @@ module FalkorLib #:nodoc:
204
230
  exit_status
205
231
  end
206
232
 
207
- ## List the files currently version
233
+ ## List the files currently under version
208
234
  def list_files(path = Dir.pwd)
209
235
  g = MiniGit.new(path)
210
236
  g.capturing.ls_files.split
211
237
  end
212
238
 
213
239
  ## Add a file/whatever to Git and commit it
214
- def add(path, msg = "")
240
+ # Supported options:
241
+ # * :force [boolean]: force the add
242
+ def add(path, msg = "", options = {})
215
243
  exit_status = 0
216
244
  dir = File.realpath File.dirname(path)
217
245
  root = rootdir(path)
218
246
  relative_path_to_root = Pathname.new( File.realpath(path) ).relative_path_from Pathname.new(root)
219
247
  real_msg = (msg.empty? ? "add '#{relative_path_to_root}'" : msg)
248
+ opts = '-f' if options[:force]
220
249
  Dir.chdir( dir ) do
221
250
  exit_status = run %{
222
- git add #{path}
251
+ git add #{opts} #{path}
223
252
  git commit -s -m "#{real_msg}" #{path}
224
253
  }
225
254
  end
@@ -241,7 +270,7 @@ module FalkorLib #:nodoc:
241
270
  g = MiniGit.new(path)
242
271
  # git rev-list --tags --max-count=1)
243
272
  a = g.capturing.rev_list :tags => true, :max_count => 1
244
- a
273
+ a
245
274
  end # last_tag_commit
246
275
 
247
276
  ## List of Git remotes
@@ -255,25 +284,29 @@ module FalkorLib #:nodoc:
255
284
  return ! remotes(path).empty?
256
285
  end
257
286
 
258
- ## Initialize git subtrees from the configuration
259
- def submodule_init(path = Dir.pwd)
287
+ ###
288
+ # Initialize git submodule from the configuration
289
+ ##
290
+ def submodule_init(path = Dir.pwd, submodules = FalkorLib.config.git[:submodules], options = {})
260
291
  exit_status = 1
261
292
  git_root_dir = rootdir(path)
262
293
  if File.exists?("#{git_root_dir}/.gitmodules")
263
- unless FalkorLib.config.git[:submodules].empty?
294
+ unless submodules.empty?
264
295
  # TODO: Check if it contains all submodules of the configuration
265
296
  end
266
297
  end
267
298
  #ap FalkorLib.config.git
268
299
  Dir.chdir(git_root_dir) do
269
300
  exit_status = FalkorLib::Git.submodule_update( git_root_dir )
270
- FalkorLib.config.git[:submodules].each do |subdir,conf|
301
+ submodules.each do |subdir,conf|
271
302
  next if conf[:url].nil?
272
303
  url = conf[:url]
273
304
  dir = "#{FalkorLib.config.git[:submodulesdir]}/#{subdir}"
274
305
  branch = conf[:branch].nil? ? 'master' : conf[:branch]
275
- unless File.directory?( dir )
276
- info "Adding Git submodule '#{dir}' from '#{url}'"
306
+ if File.directory?( dir )
307
+ puts " ... the git submodule '#{subdir}' is already setup."
308
+ else
309
+ info "adding Git submodule '#{dir}' from '#{url}'"
277
310
  exit_status = run %{
278
311
  git submodule add -b #{branch} #{url} #{dir}
279
312
  git commit -s -m "Add Git submodule '#{dir}' from '#{url}'" .gitmodules #{dir}
@@ -289,6 +322,7 @@ module FalkorLib #:nodoc:
289
322
  execute_in_dir(rootdir(path),
290
323
  %{
291
324
  git submodule init
325
+ git submodule foreach git fetch
292
326
  git submodule update
293
327
  })
294
328
  end