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)


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 156

def adddep(dep)
  say "adding dep #{dep.name}." if Gem.configuration.really_verbose
  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 }

  say " total deps of #{full_name}: #{deps.length}" if Gem.configuration.really_verbose

  # recurse while collecting deps
  dep_specs = deps.map { |dep| adddep(dep) }

  @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



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
152
# File 'lib/nix/gem-nix-command.rb', line 96

def exec
  begin
    @prerelease = false;

    args = options[:args];

    @gems_with_deps = {}
    @seen = {}

    # args to dep informations
    args.each { |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

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

    say " total: #{@gems_with_deps.length}" if Gem.configuration.really_verbose

    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 various rubygems sources, added caching to speedup subsequent executions TODO: develop a patch to rubygems sources that will increase speed for all users



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/nix/gem-nix-command.rb', line 184

def find_gem_with_source(dep)
  if @specs_by_name.nil? or @specs_by_name.empty?
    @specs_by_name = { }
    f = Gem::SpecFetcher.fetcher
    f.list(true, @prerelease).map do |source_uri, specs|
      specs.each do |spec_name, version, spec_platform|
        if Gem::Platform.match(spec_platform)
          @specs_by_name[spec_name] = [] unless @specs_by_name.key?(spec_name)
          @specs_by_name[spec_name].push([version, spec_platform, source_uri])
        end
      end
    end
  end

  found = @specs_by_name[dep.name].select do |version, spec_platform, source_uri|
    dep.match?(dep.name, version)
  end

  latest = found.sort { |a, b| a.first <=> b.first }.last
  [Gem::SpecFetcher.fetcher.fetch_spec([dep.name, latest.first, latest[1]], latest.last), latest.last.to_s]
end

#usageObject

:nodoc:



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

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