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


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

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

  define(args, &task_block)
end

Instance Attribute Details

#gh_loginObject

Returns the value of attribute gh_login.



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

def 
  
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



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
98
99
100
101
# File 'lib/puppetfile-updater/task.rb', line 31

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 = .nil? ? { } : {
      :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!

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

        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)
        aug.set("#{mpath}/ref", commits[0].sha[0...7])
      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('/', '-')
        next if !@module.nil? && @module != m.gsub(%r{.*[-/]}, '')
        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
        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