Class: WoodstovePackage

Inherits:
Object
  • Object
show all
Defined in:
lib/woodstove/packagemanager.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, site, directory, binpath) ⇒ WoodstovePackage

Returns a new instance of WoodstovePackage.



8
9
10
11
12
13
# File 'lib/woodstove/packagemanager.rb', line 8

def initialize name, site, directory, binpath
  @directory = directory
  @name = name
  @binpath = binpath
  @site = site
end

Instance Method Details

#exec_here(command) ⇒ Object

Runs a command inside the package directory.



55
56
57
# File 'lib/woodstove/packagemanager.rb', line 55

def exec_here command
  puts `cd "#{@directory}" && #{command}`
end

#exit_with_error(error) ⇒ Object

Terminate the program, throwing the specified error.



37
38
39
40
# File 'lib/woodstove/packagemanager.rb', line 37

def exit_with_error error
  puts "Error managing #{@name} in #{@directory}: #{error}"
  exit 1
end

#generate_run_command(script) ⇒ Object

Returns a command to run the specified script file.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/woodstove/packagemanager.rb', line 138

def generate_run_command script
  exit_with_error "Trying to create run command for invalid script: #{script}" if !has_script? script
  kf = kindling
  scriptfile = kf['scripts'][script]
  fullpath = "\"#{@directory}/#{kf['scripts'][script]}\""
  case scriptfile.split('.')[-1]
  when 'rb'
    "ruby #{fullpath}"
  when 'sh'
    "bash #{fullpath}"
  when 'js'
    "node #{fullpath}"
  when 'py'
    "python #{fullpath}"
  else
    fullpath
  end
end

#giturlObject

Returns the git clone / github url for this package.



43
44
45
46
47
48
49
50
51
52
# File 'lib/woodstove/packagemanager.rb', line 43

def giturl
  case @site
  when 'gitlab'
    "https://gitlab.com/#{@name}"
  when 'github'
    "https://github.com/#{@name}"
  else
    exit_with_error "Invalid site: #{@site}"
  end
end

#has_script?(script) ⇒ Boolean

Returns true if the specified script exists.

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/woodstove/packagemanager.rb', line 95

def has_script? script
  kf = kindling
  kf['scripts'] && kf['scripts'][script]
end

#install(branch) ⇒ Object

Install this package by cloning it from github.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/woodstove/packagemanager.rb', line 60

def install branch
  puts giturl
  if is_installed?
    exec_here "git init && git remote add origin \"#{giturl}\"" if !is_repo?
    exec_here "git pull --all"
  else
    `git clone -b #{branch} "#{giturl}" "#{@directory}"`
  end
  exec_here "git checkout #{branch}"
  if is_package?
    install_dependencies
    install_bins
  end
  install_external_deps
end

#install_binsObject

Puts the binaries for this package on the binpath.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/woodstove/packagemanager.rb', line 101

def install_bins
  kf = kindling
  if kf['bin'] != nil
    kf['bin'].each do |bin|
      # Create and make the script file executable.
      FileUtils.mkdir_p @binpath
      FileUtils.touch "#{@binpath}/#{bin[0]}"
      `chmod +x "#{@binpath}/#{bin[0]}"`

      # Insert a run command into the script file.
      File.open "#{@binpath}/#{bin[0]}", 'w' do |file|
        file.puts generate_run_command bin[1]
      end
    end
  end
  self
end

#install_dependenciesObject

Installs the dependencies for this package inside the ‘kindling` directory.



84
85
86
87
88
89
90
91
92
# File 'lib/woodstove/packagemanager.rb', line 84

def install_dependencies
  kf = kindling
  if kf['depends'] != nil
    kf['depends'].each do |package|
      install_package package, "#{@directory}/kindling", "#{@directory}/kindling/.bin"
    end
  end
  self
end

#install_external_depsObject

Installs dependencies for other package managers if present.



131
132
133
134
135
# File 'lib/woodstove/packagemanager.rb', line 131

def install_external_deps
  exec_here 'npm i' if File.exists? "#{@directory}/package.json"
  exec_here 'bundle install' if File.exists? "#{@directory}/Gemfile"
  self
end

#is_installed?Boolean

Returns true if the package directory exists.

Returns:

  • (Boolean)


21
22
23
# File 'lib/woodstove/packagemanager.rb', line 21

def is_installed?
  File.exists? "#{@directory}"
end

#is_package?Boolean

Returns true if the package and kindling.yaml file exist.

Returns:

  • (Boolean)


16
17
18
# File 'lib/woodstove/packagemanager.rb', line 16

def is_package?
  File.exists? "#{@directory}/kindling.yaml"
end

#is_repo?Boolean

Returns true if the package directory is a valid git repository.

Returns:

  • (Boolean)


26
27
28
# File 'lib/woodstove/packagemanager.rb', line 26

def is_repo?
  File.exists? "#{@directory}/.git"
end

#kindlingObject

Returns the kindling.yaml data for this package.



31
32
33
34
# File 'lib/woodstove/packagemanager.rb', line 31

def kindling
  exit_with_error 'Missing kindlingfile.' if !is_package?
  YAML.load File.read("#{@directory}/kindling.yaml")
end

#removeObject

Removes this package.



77
78
79
80
81
# File 'lib/woodstove/packagemanager.rb', line 77

def remove
  exit_with_error "Packages must be installed to be removed." if !is_installed?
  remove_bins if is_package?
  FileUtils.rm_rf @directory
end

#remove_binsObject

Removes the binaries for this package from the binpath.



120
121
122
123
124
125
126
127
128
# File 'lib/woodstove/packagemanager.rb', line 120

def remove_bins
  kf = kindling
  if kf['bin'] != nil
    kf['bin'].each do |bin|
      FileUtils.rm "#{@binpath}/#{bin[0]}"
    end
  end
  self
end

#run_script(script, args) ⇒ Object

Runs the specified script with the specified arguments.



158
159
160
161
# File 'lib/woodstove/packagemanager.rb', line 158

def run_script script, args
  exit_with_error "Trying to run invalid script: #{script}" if !has_script? script
  exec "#{generate_run_command script} #{args}"
end