Class: Gem::Commands::NixCommand

Inherits:
Gem::Command
  • Object
show all
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
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubygems_plugin.rb', line 5

def initialize
  defaults = Gem::DependencyInstaller::DEFAULT_OPTIONS.merge({
    :system_generated_nix => ENV.fetch('NIXPKGS_ALL', '/etc/nixos/nixpkgs') + '/pkgs/development/interpreters/ruby/generated.nix',
    :user_generated_nix => ENV['HOME'] + '/.nixpkgs/gems/generated.nix',
    :user_install => true,
    :nix_file => nil,
    :ignore_existing => false
  })
  super 'nix', 'Create a nix file containing expressions of the gems', defaults

  add_option('--nix-file=/path/to/generated.nix', 'Use specified nix file instead of default one') do |value, options|
    options[:nix_file] = value
  end

  add_option('--[no-]user-install',
             "Which generated.nix file to use:",
             "  * user-specific (#{defaults[:user_generated_nix]})",
             "  * system-wide (#{defaults[:system_generated_nix]})"
	  ) do |value, options|
    options[:user_install] = value
  end

  add_option('--[no-]ignore-existing', 'Ignore gems from the previous run') do |value, options|
    options[:ignore_existing] = value
  end
end

Instance Method Details

#adddep(dep) ⇒ Object

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


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

def adddep(dep)
  say "adding dep #{dep.name}." if Gem.configuration.really_verbose
  spec, source_uri = find_gem_with_source(dep)
  full_name = spec.full_name

  return if @gems_with_deps.key?(full_name)
  @gems_with_deps[full_name] = nil # 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 = spec.dependencies.find_all { |d| d.type != :development }

  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] = [
    spec, source_uri, dep_specs
  ]
  spec # only return spec, no source for dep list
end

#argumentsObject

:nodoc:


45
46
47
# File 'lib/rubygems_plugin.rb', line 45

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

#defaults_strObject

:nodoc:


49
50
51
52
# File 'lib/rubygems_plugin.rb', line 49

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

#descriptionObject

:nodoc:


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

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


81
82
83
84
85
86
87
88
89
90
91
92
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
152
153
154
155
156
# File 'lib/nix/gem-nix-command.rb', line 81

def exec
  begin
    @prerelease = false;

    options[:nix_file] = options[:nix_file] ||
      (options[:user_install] ? options[:user_generated_nix] : options[:system_generated_nix])

    args = options[:args]

    if FileTest.exists?(options[:nix_file])
      raise "File #{options[:nix_file]} is not a regular file" unless FileTest.file?(options[:nix_file])
      File.open(options[:nix_file], 'r') do |f|
        l = f.readlines.select {|s| s =~ /gem_nix_args =/}.first
        args += l.
          sub(/ *gem_nix_args = \[ (.*) \];/, '\1').split(' ').
          map do |s|
            s.sub(/\A''/, '').sub(/''\Z/, '')
          end
      end unless options[:ignore_existing]
    end

    args.sort!
    args.uniq!

    say "Will generate nix file for the following gems: #{args.join ', '}" if Gem::configuration.really_verbose

    @gems_with_deps = {}

    # 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

    # 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

    File.open(options[:nix_file], 'w') do |f|
      f << "# WARNING: automatically generated file\n"
      f << "# Generated by 'gem nix' command that comes from 'nix' gem\n"
      f << "g: # Get dependencies from patched gems\n"
      f << ({
        :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) << "\n"
    end
    exit_code = 0

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

#executeObject


54
55
56
57
# File 'lib/rubygems_plugin.rb', line 54

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

Raises:

  • (Gem::CommandLineError)

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

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

  raise "#{dep.name} not found!" unless @specs_by_name.key?(dep.name)

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

  raise Gem::CommandLineError, "couldn't find #{dep}" if found.empty?

  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:


41
42
43
# File 'lib/rubygems_plugin.rb', line 41

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