Class: PuppetfileUpdater::RakeTask

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/puppetfile-updater/task.rb

Overview

Public: A Rake task that can be loaded and used with everything you need.

Examples

require 'librarian-sync-puppet'
PuppetfileUpdater::RakeTask.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &task_block) ⇒ RakeTask

Public: Initialise a new PuppetfileUpdater::RakeTask.

Example

PuppetfileUpdater::RakeTask.new


26
27
28
29
30
# File 'lib/puppetfile-updater/task.rb', line 26

def initialize(*args, &task_block)
  @name = args.shift || :lint

  define(args, &task_block)
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



19
20
21
# File 'lib/puppetfile-updater/task.rb', line 19

def debug
  @debug
end

#gh_loginObject

Returns the value of attribute gh_login.



17
18
19
# File 'lib/puppetfile-updater/task.rb', line 17

def 
  @gh_login
end

#gh_passwordObject

Returns the value of attribute gh_password.



18
19
20
# File 'lib/puppetfile-updater/task.rb', line 18

def gh_password
  @gh_password
end

#majorObject

Returns the value of attribute major.



16
17
18
# File 'lib/puppetfile-updater/task.rb', line 16

def major
  @major
end

#moduleObject

Returns the value of attribute module.



15
16
17
# File 'lib/puppetfile-updater/task.rb', line 15

def module
  @module
end

#userObject

Returns the value of attribute user.



14
15
16
# File 'lib/puppetfile-updater/task.rb', line 14

def user
  @user
end

Instance Method Details

#define(args, &task_block) ⇒ Object



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/puppetfile-updater/task.rb', line 32

def define(args, &task_block)
  desc 'Update module references in the Puppetfile'

  task_block.call(*[self, args].slice(0, task_block.arity)) if task_block

  # clear any (auto-)pre-existing task
  Rake::Task[@name].clear if Rake::Task.task_defined?(@name)

  task @name do
    require 'augeas'
    require 'octokit'
    require 'puppet_forge'

    @user ||= '.*'

    gh_opts = @gh_login.nil? ? { } : {
      :login    => @gh_login,
      :password => @gh_password,
    }
    github = Octokit::Client.new gh_opts

    libdir = File.dirname(__FILE__)
    lens_dir = File.expand_path(File.join(libdir, '..', '..', 'augeas', 'lenses'))
    Augeas.open(Dir.pwd, lens_dir, Augeas::NO_MODL_AUTOLOAD) do |aug|
      aug.transform(
        :incl => '/Puppetfile',
        :excl => [],
        :lens => 'Puppetfile.lns',
        :name => 'Puppetfile',
      )
      aug.load!

      error_path = '/augeas/files/Puppetfile/error'
      unless aug.match('/augeas/files/Puppetfile/error').size == 0
        msg = "Failed to parse Puppetfile at line #{aug.get(error_path+'/line')}, "
        msg << "character #{aug.get(error_path+'/char')}: "
        msg << aug.get('/augeas/files/Puppetfile/error/message')
        abort msg
      end

      # Update from GitHub
      aug.match("/files/Puppetfile/*[git=~regexp('.*/#{@user}/.*')]").each do |mpath|
        m = aug.get(mpath)
        puts "D: Considering #{m} for git update" if @debug
        next if !@module.nil? && @module != m.gsub(%r{.*[-/]}, '')
        puts "D: #{m} selected by filters" if @debug

        warn "W: #{m} is a fork!" unless m =~ /#{@user}/

        git_url = aug.get("#{mpath}/git")
        repo = Octokit::Repository.from_url(git_url.gsub(/\.git$/, ''))
        commits = github.commits(repo)
        ref = commits[0].sha[0...7]
        puts "D: New ref for #{m} is #{ref}" if @debug
        aug.set("#{mpath}/ref", ref)
      end

      # Update from Forge
      PuppetForge.user_agent = 'Puppetfile-Updater/0.1.0'
      aug.match("/files/Puppetfile/*[label()!='#comment' and .=~regexp('#{@user}/.*') and @version]").each do |mpath|
        m = aug.get(mpath).gsub('/', '-')
        puts "D: Considering #{m} for forge update" if @debug
        next if !@module.nil? && @module != m.gsub(%r{.*[-/]}, '')
        puts "D: #{m} selected by filters"
        v = aug.get("#{mpath}/@version")
        forge_m = PuppetForge::Module.find(m)
        release = forge_m.releases.select { |r| r.deleted_at.nil? }[0]
        new_v = release.version
        puts "D: New version for #{m} is #{new_v}" if @debug
        if new_v.split('.')[0] != v.split('.')[0]
          if @major
            warn "W: #{m} has incompatible changes between #{v} and #{new_v}"
            aug.set("#{mpath}/@version", new_v)
          else
            warn "W: Not upgrading #{m} from #{v} to new major version #{new_v}"
          end
        else
          warn "W: #{m} got new features between #{v} and #{new_v}" if new_v.split('.')[1] != v.split('.')[1]
          aug.set("#{mpath}/@version", new_v)
        end
      end

      aug.save!
    end
  end
end