Module: FalkorLib::Bootstrap

Defined in:
lib/falkorlib/bootstrap/base.rb

Class Method Summary collapse

Class Method Details

.get_badge(subject, status, color = 'blue', options = {}) ⇒ Object

get_badge ###### Return a Markdown-formatted string for a badge to display, typically in a README. Based on shields.io/ Supported options:

* :style [string] style of the badge, Elligible: ['plastic', 'flat', 'flat-square']


497
498
499
500
501
502
# File 'lib/falkorlib/bootstrap/base.rb', line 497

def get_badge(subject, status, color = 'blue', options = {})
    st = status.gsub(/-/, '--').gsub(/_/, '__')
    res = "https://img.shields.io/badge/#{subject}-#{st}-#{color}.svg"
    res += "?style=#{options[:style]}" if options[:style]
    res
end

.get_project_name(dir = Dir.pwd, options = {}) ⇒ Object

get_project_name ###### Return a “reasonable” project name from a given [sub] directory i.e. its basename



507
508
509
510
511
# File 'lib/falkorlib/bootstrap/base.rb', line 507

def get_project_name(dir = Dir.pwd, options = {})
    path = normalized_path(dir)
    path = FalkorLib::Git.rootdir(path) if FalkorLib::Git.init?(path)
    File.basename(path)
end

.motd(dir = Dir.pwd, options = {}) ⇒ Object

motd ###### Generate a new motd (Message of the Day) file Supported options:

* :force [boolean] force action


345
346
347
348
349
350
351
352
353
354
# File 'lib/falkorlib/bootstrap/base.rb', line 345

def motd(dir = Dir.pwd, options = {})
    config = FalkorLib::Config::Bootstrap::DEFAULTS[:motd].merge!(options)
    path = normalized_path(dir)
    erbfile = File.join( FalkorLib.templates, 'motd', 'motd.erb')
    outfile = (options[:file] =~ /^\//) ? options[:file] : File.join(path, options[:file])
    info "Generate a motd (Message of the Day) file '#{outfile}'"
    config[:os] = Facter.value(:lsbdistdescription) if Facter.value(:lsbdistdescription)
    config[:os] = "Mac " + Facter.value(:sp_os_version) if Facter.value(:sp_os_version)
    write_from_erb_template(erbfile, outfile, config, options)
end

.readme(dir = Dir.pwd, options = {}) ⇒ Object

readme ###### Bootstrap a README file for various context Supported options:

* :no_interaction [boolean]: do not interact
* :force     [boolean] force overwritting
* :latex     [boolean] describe a LaTeX project
* :octopress [boolean] octopress site


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/falkorlib/bootstrap/base.rb', line 366

def readme(dir = Dir.pwd, options = {})
    info "Bootstrap a README file for this project"
    # get the local configuration
    local_config = FalkorLib::Config.get(dir)
    config = FalkorLib::Config::Bootstrap::DEFAULTS[:metadata].clone
    if local_config[:project]
        config.deep_merge!( local_config[:project])
    else
        config[:name]     = ask("\tProject name: ", get_project_name(dir)) unless options[:name]
    end
    # Type of project
    config[:type] << :latex if options[:latex]
    if config[:type].empty?
        t = select_from( FalkorLib::Config::Bootstrap::DEFAULTS[:types],
                        'Select the type of project to describe:', 1)
        config[:type] << t
        config[:type] << [ :ruby, :rvm ] if [ :gem, :rvm, :octopress, :puppet_module ].include?( t )
        config[:type] << :python if t == :pyenv
    end
    config[:type] = config[:type].uniq.flatten
    # Apply options (if provided)
    [ :name, :forge ].each do |k|
        config[k.to_sym] = options[k.to_sym] if options[k.to_sym]
    end
    path = normalized_path(dir)
    config[:filename] = options[:filename] ? options[:filename] : File.join(path, 'README.md')
    config[:forge] = select_forge(config[:forge]).to_sym if config[:forge].empty?
    forges = FalkorLib::Config::Bootstrap::DEFAULTS[:forge][ config[:forge].to_sym ]
    default_source = case config[:forge]
                     when :gforge
                         forges[:url] + "/projects/" + config[:name].downcase
                     when :github
                         forges[:url] + "/" + forges[:login] + "/" + config[:name].downcase
                     when :gitlab
                         forges[:url] + "/" + forges[:name].downcase
                     else
                         ""
                     end
    ap config
    FalkorLib::Config::Bootstrap::DEFAULTS[:metadata].each do |k,v|
        next if v.kind_of?(Array) or [ :license, :forge ].include?( k )
        next if k == :name and ! config[:name].empty?
        next if k == :issues_url and ! [ :github, :gitlab ].include?( config[:forge] )
        #next unless [ :name, :summary, :description ].include?(k.to_sym)
        default_answer = case k
                         when :description
                             config[:description].empty? ? "#{config[:summary]}" : "#{config[:description]}"
                         when :source
                             config[:source].empty? ? default_source : "#{config[:source]}"
                         when :project_page
                             config[:source].empty? ? v : config[:source]
                         when :issues_url
                             config[:project_page].empty? ? v : "#{config[:project_page]}/issues"
                         else
                             config[k.to_sym].empty? ? v : config[k.to_sym]
                         end
        config[k.to_sym] = ask( "\t" + sprintf("Project %-20s", "#{k}"), default_answer)
    end
    tags = ask("\tKeywords (comma-separated list of tags)", config[:tags].join(','))
    config[:tags]    = tags.split(',')
    config[:license] = select_licence() if config[:license].empty?
    # stack the ERB files required to generate the README
    templatedir = File.join( FalkorLib.templates, 'README')
    erbfiles = [ 'header_readme.erb',  ]
    [ :latex ].each do |type|
        erbfiles << "readme_#{type}.erb" if options[type.to_sym] and File.exist?( File.join(templatedir, "readme_#{type}.erb"))
    end
    erbfiles << "readme_git.erb"     if FalkorLib::Git.init?(dir)
    erbfiles << "readme_gitflow.erb" if FalkorLib::GitFlow.init?(dir)
    erbfiles << "readme_rvm.erb"     if config[:type].include?(:rvm)
    erbfiles << "footer_readme.erb"

    content = ""
    erbfiles.each do |f|
        erbfile = File.join(templatedir, f)
        content += ERB.new(File.read("#{erbfile}"), nil, '<>').result(binding)
    end
    show_diff_and_write(content, config[:filename], options)

    # Eventually save/upgrade local config
    info "=> saving customization of the FalkorLib configuration in #{FalkorLib.config[:config_files][:local]}"
    really_continue?
    FalkorLib::Config::Bootstrap::DEFAULTS[:metadata].keys.each do |k|
        local_config[:project] = {} unless local_config[:project]
        local_config[:project][k.to_sym] = config[k.to_sym]
    end
    if FalkorLib::GitFlow.init?(dir)
        local_config[:gitflow]  = {} unless local_config[:gitflow]
        local_config[:gitflow][:branches] = FalkorLib.config[:gitflow][:branches].clone unless local_config[:gitflow][:branches]
        [ :master, :develop ].each do |b|
            local_config[:gitflow][:branches][b.to_sym] = FalkorLib::GitFlow.branches(b.to_sym)
        end
    end
    FalkorLib::Config.save(dir, local_config, :local)
    #
end

.repo(name, options = {}) ⇒ Object

repo ###### Initialize a Git repository for a project with my favorite layout Supported options:

  • :no_interaction [boolean]: do not interact

  • :gitflow [boolean]: bootstrap with git-flow

  • :interactive [boolean] Confirm Gitflow branch names

  • :master [string] Branch name for production releases

  • :develop [string] Branch name for development commits

  • :make [boolean] Use a Makefile to pilot the repository actions

  • :rake [boolean] Use a Rakefile (and FalkorLib) to pilot the repository action

  • :remote_sync [boolean] Operate a git remote synchronization

  • :latex [boolean] Initiate a LaTeX project

  • :gem [boolean] Initiate a Ruby gem project

  • :rvm [boolean] Initiate a RVM-based Ruby project

  • :pyenv [boolean] Initiate a pyenv-based Python project

  • :octopress [boolean] Initiate an Octopress web site



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/falkorlib/bootstrap/base.rb', line 205

def repo(name, options = {})
    ap options if options[:debug]
    path    = normalized_path(name)
    project = File.basename(path)
    use_git = FalkorLib::Git.init?(path)
    options[:make] = false if options[:rake]
    info "Bootstrap a [Git] repository for the project '#{project}'"
    if use_git
        warning "Git is already initialized for the repository '#{name}'"
        really_continue? unless options[:force]
    end
    if options[:git_flow]
        info " ==> initialize Git flow in #{path}"
        FalkorLib::GitFlow.init(path, options)
        gitflow_branches = {}
        [ :master, :develop ].each do |t|
            gitflow_branches[t.to_sym] = FalkorLib::GitFlow.branches(t, path)
        end
    else
        FalkorLib::Git.init(path, options)
    end
    # === prepare Git submodules ===
    info " ==> prepare the relevant Git submodules"
    submodules = {
                  'gitstats' => { :url => 'https://github.com/hoxu/gitstats.git' }
                 }
    if options[:make]
        submodules['Makefiles'] = {
                                   :url    => 'https://github.com/Falkor/Makefiles.git',
                                   :branch => 'devel'
                                  }
    end
    FalkorLib::Git.submodule_init(path, submodules)
    # === Prepare root [M|R]akefile ===
    if options[:make]
        info " ==> prepare Root Makefile"
        makefile = File.join(path, "Makefile")
        unless File.exist?( makefile )
            src_makefile = File.join(path, FalkorLib.config.git[:submodulesdir],
                                     'Makefiles', 'repo', 'Makefile')
            FileUtils.cp src_makefile, makefile
            info "adapting Makefile to the gitflow branches"
            Dir.chdir( path ) do
                run %{
   sed -i '' \
-e \"s/^GITFLOW_BR_MASTER=production/GITFLOW_BR_MASTER=#{gitflow_branches[:master]}/\" \
-e \"s/^GITFLOW_BR_DEVELOP=devel/GITFLOW_BR_DEVELOP=#{gitflow_branches[:develop]}/\" \
Makefile
                }
            end
            FalkorLib::Git.add(makefile, 'Initialize root Makefile for the repo')
        else
            puts "  ... not overwriting the root Makefile which already exists"
        end
    end
    if options[:rake]
        info " ==> prepare Root Rakefile"
        rakefile = File.join(path, "Rakefile")
        unless File.exist?( rakefile )
            templatedir = File.join( FalkorLib.templates, 'Rakefile')
            erbfiles = [ 'header_rakefile.erb' ]
            erbfiles << 'rakefile_gitflow.erb' if FalkorLib::GitFlow.init?(path)
            erbfiles << 'footer_rakefile.erb'
            write_from_erb_template(erbfiles, rakefile, {}, { :srcdir => "#{templatedir}" })
        end
    end

    # === VERSION file ===
    FalkorLib::Bootstrap.versionfile(path, :tag => 'v0.0.0')

    # === RVM ====
    FalkorLib::Bootstrap.rvm(path, options) if options[:rvm]

    # === README ===
    FalkorLib::Bootstrap.readme(path, options)

    #===== remote synchro ========
    if options[:remote_sync]
        remotes  = FalkorLib::Git.remotes(path)
        if remotes.include?( 'origin' )
            info "perform remote synchronization"
            [ :master, :develop ].each do |t|
                FalkorLib::Git.publish(gitflow_branch[t.to_sym], path, 'origin')
            end
        else
            warning "no Git remote  'origin' found, thus no remote synchronization performed"
        end
    end

end

.rvm(dir = Dir.pwd, options = {}) ⇒ Object

rvm ###### Initialize RVM in the current directory Supported options:

* :force       [boolean] force overwritting
* :ruby        [string]  Ruby version to configure for RVM
* :versionfile [string]  Ruby Version file
* :gemset      [string]  RVM Gemset to configure
* :gemsetfile  [string]  RVM Gemset file
* :commit      [boolean] Commit the changes NOT YET USED


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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/falkorlib/bootstrap/base.rb', line 138

def rvm(dir = Dir.pwd, options = {})
    info "Initialize Ruby Version Manager (RVM)"
    ap options if options[:debug]
    path = normalized_path(dir)
    use_git = FalkorLib::Git.init?(path)
    rootdir = use_git ? FalkorLib::Git.rootdir(path) : path
    files = {}
    exit_status = 1
    [:versionfile, :gemsetfile].each do |type|
        f = options[type.to_sym].nil? ? FalkorLib.config[:rvm][type.to_sym] : options[type.to_sym]
        if File.exists?( File.join( rootdir, f ))
            content = `cat #{File.join( rootdir, f)}`.chomp
            warning "The RVM file '#{f}' already exists (and contains '#{content}')"
            next unless options[:force]
            warning "... and it WILL BE overwritten"
        end
        files[type.to_sym] = f
    end
    # ==== Ruby version ===
    unless files[:versionfile].nil?
        file = File.join(rootdir, files[:versionfile])
        v =
          options[:ruby] ?
          options[:ruby] :
          select_from(FalkorLib.config[:rvm][:rubies],
                      "Select RVM ruby to configure for this directory",
                      1)
        info " ==>  configuring RVM version file '#{files[:versionfile]}' for ruby version '#{v}'"
        File.open(file, 'w') do |f|
            f.puts v
        end
        exit_status = (File.exists?(file) and `cat #{file}`.chomp == v) ? 0 : 1
        FalkorLib::Git.add(File.join(rootdir, files[:versionfile])) if use_git
    end
    # === Gemset ===
    if files[:gemsetfile]
        file = File.join(rootdir, files[:gemsetfile])
        default_gemset = File.basename(rootdir)
        default_gemset = `cat #{file}`.chomp if File.exists?( file )
        g = options[:gemset] ? options[:gemset] : ask("Enter RVM gemset name for this directory", default_gemset)
        info " ==>  configuring RVM gemset file '#{files[:gemsetfile]}' with content '#{g}'"
        File.open( File.join(rootdir, files[:gemsetfile]), 'w') do |f|
            f.puts g
        end
        exit_status = (File.exists?(file) and `cat #{file}`.chomp == g) ? 0 : 1
        FalkorLib::Git.add(File.join(rootdir, files[:gemsetfile])) if use_git
    end
    exit_status
end

.select_forge(default = :gforge, options = {}) ⇒ Object

Select the forge (gforge, github, etc.) hosting the project sources



466
467
468
469
470
471
472
473
474
475
476
# File 'lib/falkorlib/bootstrap/base.rb', line 466

def select_forge(default = :gforge, options = {})
    forge = FalkorLib::Config::Bootstrap::DEFAULTS[:forge]
    #ap forge
    default_idx = forge.keys.index(default)
    default_idx = 0 if default_idx.nil?
    v = select_from(forge.map{ |k,v| v[:name] },
                    "Select the Forge hosting the project sources",
                    default_idx+1,
                    forge.keys)
    v
end

.select_licence(default_licence = , options = {}) ⇒ Object

select_licence ###### Select a given licence for the project



481
482
483
484
485
486
487
488
489
# File 'lib/falkorlib/bootstrap/base.rb', line 481

def select_licence(default_licence = FalkorLib::Config::Bootstrap::DEFAULTS[:metadata][:license],
                   options = {})
    list_license    = FalkorLib::Config::Bootstrap::DEFAULTS[:licenses].keys
    idx = list_license.index(default_licence) unless default_licence.nil?
    select_from(list_license,
                'Select the license index for this project:',
                idx.nil? ? 1 : idx + 1)
    #licence
end

.trash(path = Dir.pwd, dirname = , options = {}) ⇒ Object

Initialize a trash directory in path



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/falkorlib/bootstrap/base.rb', line 104

def trash(path = Dir.pwd, dirname = FalkorLib.config[:templates][:trashdir], options = {})
    #args = method(__method__).parameters.map { |arg| arg[1].to_s }.map { |arg| { arg.to_sym => eval(arg) } }.reduce Hash.new, :merge
    #ap args
    exit_status = 0
    trashdir = File.join(path, dirname)
    if Dir.exists?(trashdir)
        warning "The trash directory '#{dirname}' already exists"
        return 1
    end
    Dir.chdir(path) do
        info "creating the trash directory '#{dirname}'"
        exit_status = run %{
  mkdir -p #{dirname}
  echo '*' > #{dirname}/.gitignore
        }
        if FalkorLib::Git.init?(path)
            exit_status = FalkorLib::Git.add(File.join(path, dirname, '.gitignore' ),
                                             'Add Trash directory',
                                             { :force => true } )
        end
    end
    exit_status.to_i
end

.versionfile(dir = Dir.pwd, options = {}) ⇒ Object

versionfile ###### Bootstrap a VERSION file at the root of a project Supported options:

  • :file [string] filename

  • :version [string] version to mention in the file



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/falkorlib/bootstrap/base.rb', line 302

def versionfile(dir = Dir.pwd, options = {})
    file    = options[:file]    ? options[:file]    : 'VERSION'
    version = options[:version] ? options[:version] : '0.0.0'
    info " ==> bootstrapping a VERSION file"
    path = normalized_path(dir)
    path = FalkorLib::Git.rootdir(path) if FalkorLib::Git.init?(path)
    unless Dir.exists?( path )
        warning "The directory #{path} does not exists and will be created"
        really_continue?
        FileUtils.mkdir_p path
    end
    versionfile = File.join(path, file)
    unless File.exists?( versionfile )
        FalkorLib::Versioning.set_version(version, path, {
                                                          :type => 'file',
                                                          :source => { :filename => file }
                                                         })
        Dir.chdir( path ) do
            run %{ git tag #{options[:tag]} } if options[:tag]
        end
    else
        puts "  ... not overwriting the #{file} file which already exists"
    end

    # unless File.exists?( versionfile )
    #     run %{  echo "#{version}" > #{versionfile} }
    #     if FalkorLib::Git.init?(path)
    #         FalkorLib::Git.add(versionfile, "Initialize #{file} file")
    #         Dir.chdir( path ) do
    #             run %{ git tag #{options[:tag]} } if options[:tag]
    #         end
    #     end
    # else
    #     puts "  ... not overwriting the #{file} file which already exists"
    # end
end