Class: Gem::Commands::NixCommand

Inherits:
Gem::Command
  • Object
show all
Includes:
InstallUpdateOptions, LocalRemoteOptions, VersionOption
Defined in:
lib/nix/gem-nix-command.rb,
lib/rubygems_plugin.rb

Instance Method Summary collapse

Constructor Details

#initializeNixCommand

Returns a new instance of NixCommand.



5
6
7
8
# File 'lib/rubygems_plugin.rb', line 5

def initialize
  defaults = Gem::DependencyInstaller::DEFAULT_OPTIONS.merge({ })
  super 'nix', 'Create a nix file containing expressions of the gems', defaults
end

Instance Method Details

#adddep(dep) ⇒ Object

helper funtions ================

Raises:

  • (Gem::CommandLineError)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/nix/gem-nix-command.rb', line 155

def adddep(dep)
  gem = find_gem_with_source(dep)
  raise Gem::CommandLineError, "couldn't find #{dep}" if gem.nil?
  full_name = gem[0].full_name

  return if @seen[full_name]
  @seen[full_name] = true # there maybe circular dependencies. thus mark this gem seen as early as possible

  # development deps can't be found. Some are old. Thus only add rutime dependencies
  deps = gem[0].dependencies.find_all { |d| d.type == :runtime }

  warn " total deps of #{full_name}: #{deps.length}"

  dep_specs = []
  # recurse while collecting deps
  deps.each {|dep_var| dep_specs.push(adddep(dep_var)) }


  @gems_with_deps[full_name] = [
    gem[0], # spec
    gem[1], # src
    dep_specs # deps
  ]
  gem[0] # only return spec, no source for dep list
end

#argumentsObject

:nodoc:



23
24
25
# File 'lib/rubygems_plugin.rb', line 23

def arguments # :nodoc:
  "GEMNAME       name of gem to be added to the expressions file"
end

#defaults_strObject

:nodoc:



27
28
29
30
# File 'lib/rubygems_plugin.rb', line 27

def defaults_str # :nodoc:
  # what to put in here ? TODO (probably nothing is ok)
  ""
end

#descriptionObject

:nodoc:



10
11
12
13
14
15
16
17
# File 'lib/rubygems_plugin.rb', line 10

def description # :nodoc:
  <<-EOF
    create a nix file containing expressions of the gems
    There are many gems. So it's best to only specify some target gems and
    take them into acocunt with their deps
    TODO more details
  EOF
end

#execObject



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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/nix/gem-nix-command.rb', line 93

def exec
  begin
    @prerelease = false;

    args = options[:args];

    @gems_with_deps = {}
    @seen = {}

    # args to dep informations
    args.map { |arg|
      if arg =~ /(.+)-?(.*)?/ then
        gem_name = $1
        version =  $2.empty? ?  Gem::Requirement.default : Gem::Version.new($2)
      else
        raise Gem::CommandLineError, "couldn't parse arg. expected: name or name-version"
      end

      warn "adding #{gem_name}\n"

      adddep(Gem::Dependency.new gem_name, version)
    }

    warn " total: #{@gems_with_deps.length}"

    print <<-EOF
# WARNING: automatically generated file
# the gem nix command comes from 'nix' gem
g: # Get dependencies from patched gems
    EOF
    # define aliases
    aliases = {}
    output_gems = {}

    @gems_with_deps.each_value do |(spec, src, deps)|
      if !aliases.key?(spec.name) or aliases[spec.name].version < spec.version
        aliases[spec.name] = spec
      end

      output_gems[spec.nix_name(:symbol)] = spec.nix_derivation.merge({
        :requiredGems => deps.compact.map { |d| d.nix_name(:rhs_sym) }
      })
    end

    print ({
      :gem_nix_args => args,
      :gems => output_gems,
      :aliases => aliases.values.inject({}) do |h, s|
        h[s.nix_name(:short_sym)] = s.nix_name(:rhs_sym)
        h
      end
    }.to_nix)
    exit_code = 0

  rescue => e
    puts e.inspect
    puts e.backtrace
  end
end

#executeObject



32
33
34
35
# File 'lib/rubygems_plugin.rb', line 32

def execute
  require 'nix/gem-nix-command' unless respond_to? :exec
	exec
end

#find_gem_with_source(dep) ⇒ Object

copied from dependency_installer, stripped



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/nix/gem-nix-command.rb', line 183

def find_gem_with_source(dep)
  gems_and_sources = []

  # no local

    requirements = dep.requirement.requirements.map do |req, ver|
      req
    end

    all = true
    found = Gem::SpecFetcher.fetcher.fetch dep, all, true, @prerelease
    found.reverse[0]
end

#usageObject

:nodoc:



19
20
21
# File 'lib/rubygems_plugin.rb', line 19

def usage # :nodoc:
  "#{program_name} GEMNAME [GEMNAME ...] [options] -- --build-flags"
end