Class: Exer::Make

Inherits:
Object
  • Object
show all
Includes:
Template
Defined in:
lib/exer/make.rb

Overview

Make class handle all the logic that we need to make binaries. Functions are written to the file and removed after go build. By default all three platforms are used, but you can also exclude each of them.

Constant Summary collapse

GO =

golang source directory path

File.expand_path(__FILE__)
.gsub('lib/exer/make.rb', 'go/bin/go').freeze
PLATFORMS =
%i[linux darwin windows].freeze

Constants included from Template

Template::BINARY_EXIST, Template::FUNCTION, Template::GEM_INSTALL, Template::GEM_RUN, Template::GO_PACKAGES, Template::MAIN_FUNCTION, Template::RUBY_EXEC, Template::RUBY_EXIST, Template::WAIT_FOR_ENTER

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Make

Prepare go file - give it a name and import packages. This is also a name of final binaries.

Examples:

app = Exer::Make.new 'my_gem_name'

Parameters:

  • filename (String)

    Required. Name of binaries.



35
36
37
38
39
40
41
# File 'lib/exer/make.rb', line 35

def initialize(filename)
  @filename = filename
  @import = FUNCTION[:go_packages]
  @main = "\nfunc main() {"
  @functions = "\n"
  @wfe = false
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



18
19
20
# File 'lib/exer/make.rb', line 18

def filename
  @filename
end

#functionsObject (readonly)

Returns the value of attribute functions.



16
17
18
# File 'lib/exer/make.rb', line 16

def functions
  @functions
end

#importObject (readonly)

Returns the value of attribute import.



16
17
18
# File 'lib/exer/make.rb', line 16

def import
  @import
end

#mainObject (readonly)

Returns the value of attribute main.



16
17
18
# File 'lib/exer/make.rb', line 16

def main
  @main
end

Instance Method Details

#add(function, arg = nil) ⇒ String Also known as: <<

Add previously defined function to go-main, to be executed.

Examples:

app.add :gem_install, 'my_gem_name'

Parameters:

  • function (Symbol)

    Required. Function name from ::Template.

  • arg (String) (defaults to: nil)

    Optional. For functions that require argument (like gem install).

Returns:

  • (String)

See Also:



70
71
72
73
74
# File 'lib/exer/make.rb', line 70

def add(function, arg = nil)
  func = String.new MAIN_FUNCTION[function]
  func.gsub!('COMMAND', arg) unless arg.nil?
  @main += "\n#{func}"
end

#add_defaultsObject

Add functions to the go file, and check if ruby is installed.



109
110
111
112
113
114
# File 'lib/exer/make.rb', line 109

def add_defaults
  %i[binary_exist ruby_exist ruby_exec gem_install gem_run].each do |x|
    add_function x
  end
  add :ruby_exist
end

#add_function(function_name) ⇒ String

Define go functions, so we can use them in main. They are defined in ::Template, and automatically added to file.

Examples:

Define function gem_install

app.add_function :gem_install

Parameters:

  • function_name (Symbol)

    Required. Name of Template::FUNCTION.

Returns:

  • (String)

See Also:



54
55
56
# File 'lib/exer/make.rb', line 54

def add_function(function_name)
  @functions += "\n#{FUNCTION[function_name]}"
end

#build(exclude = nil) ⇒ Boolean

Make executable for platforms we want. Functions are written to the file, built into binaries, and file is removed.

If you build binary only with gem_install, do not forget to add wait_for_enter, so user must press enter after installation. Don’t use with :gem_run.

Examples:

Build for Windows, Linux, Mac

app.build do |x|
  x.add_defaults
  x.add :gem_install, 'my-gem-name'
  x.wait_for_enter
end

Build gem installer and runner

app.build do |x|
  x.add :gem_run, 'my-gem-name'
end

Parameters:

  • exclude (Symbol) (defaults to: nil)

    Optional. Platforms to exclude when making binaries. Also accept array of symbols.

Returns:

  • (Boolean)


99
100
101
102
103
104
105
# File 'lib/exer/make.rb', line 99

def build(exclude = nil)
  build_binaries exclude
  true
rescue StandardError => e
  puts e.message
  false
end

#wait_for_enterObject

Wait for user to press [ENTER] before finish. Don’t use with :gem_run



119
120
121
# File 'lib/exer/make.rb', line 119

def wait_for_enter
  @wfe = true
end