Module: Neutron

Defined in:
lib/neutron.rb,
lib/neutron/clean.rb,
lib/neutron/install.rb,
lib/neutron/version.rb

Overview

Main module

Defined Under Namespace

Modules: CC, PkgStatus, Static, Valac Classes: ExecutionError, FileList, FilePair, FilePairList, PkgConf

Constant Summary collapse

VERSION =
'0.2.0'.freeze

Class Method Summary collapse

Class Method Details

.cat(*files, out, **opts) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/neutron.rb', line 42

def self.cat(*files, out, **opts)
  out = File.expand_path(out)
  
  File.delete(out) if File.exist?(out)

  f = File.open(out, 'w')
  puts "] Open: #{out}"

  if opts[:prepend]
    f.write(opts[:prepend])
    puts "] Prepended content to #{out}"
  end

  files.each do |s|
    s = File.expand_path(s)
    f.write(File.read(s))
    puts "] Write #{s} to #{out}"
  end

  f.close
  puts "] Close: #{out}"
end

.clean(*files) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/neutron/clean.rb', line 4

def self.clean(*files)
  files << Neutron::PkgStatus::FNAME
  files.map! do |file|
    File.expand_path(file)
  end
  files.each do |file|
    if File.exist?(file)
      Neutron.execute(
        "rm -r #{file}",
        must_success: true
      )
    end
  end
end

.execute(string, **opts) ⇒ Array[String,Integer]

Executes given command

Parameters:

  • string (String)

    Command to execute

  • opts (Hash)

Options Hash (**opts):

  • :must_success (Boolean)

    If exit code is not 0 - raises an exception

Returns:

  • (Array[String,Integer])

    Output and exit code



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/neutron.rb', line 112

def self.execute(string, **opts)
  puts "> #{string}"
  stdin, stdout, waiter = *Open3.popen2e(string)
  out = '' 
  while s = stdout.getc
    print s
    out << s
  end
  stdin.close
  stdout.close
  if opts[:must_success] and waiter.value.exitstatus != 0
    raise Neutron::ExecutionError, "Exitcode #{waiter.value.exitstatus} returned!"
  end
  return [out, waiter.value.exitstatus]
end

.file_to_ext(f, ext) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/neutron.rb', line 33

def self.file_to_ext(f, ext)
  m = /(.+)\..+/.match(f)
  if m
    m[1] + ext
  else
    f + ext
  end
end

.files(sources, t_ext) ⇒ Neutron::FilePairList<FilePair>

Returns list of files which need to be processed

Parameters:

  • sources (Array<String>)
  • t_ext (String)

    Target extension

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/neutron.rb', line 85

def self.files(sources, t_ext)
  targets = sources.ext(t_ext)
  pairs = sources.zip(targets)
  pairs = pairs.keep_if do |item|
    source = item[0]
    target = item[1]
    if File.exist? target
      st = File.stat(File.expand_path(source)).mtime
      tt = File.stat(File.expand_path(target)).mtime
      if st > tt
        true
      else
        false
      end
    else
      true
    end
  end
  pairs.map!{|item| Neutron::FilePair.new(item[0], item[1])}
  Neutron::FilePairList.new(pairs)
end

.install(*files, dir) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/neutron/install.rb', line 4

def self.install(*files, dir)
  prefix = if ENV['PREFIX'] then ENV['PREFIX'] else '/usr' end
  dir = File.expand_path(File.join(prefix, dir))
  sudo = if ENV['USE_SUDO'] then 'sudo ' else '' end
  unless File.exist? dir
    Neutron.execute(
      "#{sudo}mkdir --parents --mode=755 #{dir}",
      must_success: true
    )
  end
  files.each do |file|
    p file
    dn = File.join(dir, File.dirname(file))
    p dn
    #file = File.expand_path(file)
    unless File.exist?(dn)
      Neutron.execute("#{sudo}mkdir --parents --mode=755 #{dn}")
    end
    Neutron.execute(
      "#{sudo}rsync -a --relative --chmod=755 #{file} #{dir}/",
      must_success: true
    )
  end 
end