-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #486 from ChrisMacNaughton/feature/cargo
[ADDED] Experimental support for Rust dependencies with Cargo
- Loading branch information
Showing
12 changed files
with
362 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'json' | ||
|
||
module LicenseFinder | ||
class Cargo < PackageManager | ||
def current_packages | ||
cargo_output.map do |package| | ||
CargoPackage.new(package, logger: logger) | ||
end | ||
end | ||
|
||
def self.package_management_command | ||
'cargo' | ||
end | ||
|
||
def self.prepare_command | ||
'cargo fetch' | ||
end | ||
|
||
def possible_package_paths | ||
[project_path.join('Cargo.lock'), project_path.join('Cargo.toml')] | ||
end | ||
|
||
private | ||
|
||
def cargo_output | ||
command = "#{Cargo.package_management_command} metadata --format-version=1" | ||
|
||
stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(command) } | ||
raise "Command '#{command}' failed to execute: #{stderr}" unless status.success? | ||
JSON(stdout) | ||
.fetch('packages', []) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module LicenseFinder | ||
class CargoPackage < Package | ||
def initialize(crate, options = {}) | ||
crate = crate.reject { |_, v| v.nil? || v == '' } | ||
children = crate.fetch('dependencies', []).map { |p| p['name'] } | ||
licenses = crate.fetch('license', '').split('/') | ||
super( | ||
crate['name'], | ||
crate['version'], | ||
options.merge( | ||
summary: crate.fetch('description', '').strip, | ||
spec_licenses: licenses.compact, | ||
children: children | ||
) | ||
) | ||
end | ||
|
||
def package_manager | ||
'Cargo' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
{ | ||
"packages": [ | ||
{ | ||
"name": "license-finder", | ||
"version": "0.1.0", | ||
"id": "license-finder 0.1.0 (path+file:///home/test/license-finder)", | ||
"license": null, | ||
"license_file": null, | ||
"description": null, | ||
"source": null, | ||
"dependencies": [ | ||
{ | ||
"name": "log", | ||
"source": "registry+https://github.com/rust-lang/crates.io-index", | ||
"req": "*", | ||
"kind": null, | ||
"optional": false, | ||
"uses_default_features": true, | ||
"features": [], | ||
"target": null | ||
}, | ||
{ | ||
"name": "simple_logger", | ||
"source": "registry+https://github.com/rust-lang/crates.io-index", | ||
"req": "*", | ||
"kind": null, | ||
"optional": false, | ||
"uses_default_features": true, | ||
"features": [], | ||
"target": null | ||
} | ||
], | ||
"targets": [ | ||
{ | ||
"kind": [ | ||
"bin" | ||
], | ||
"crate_types": [ | ||
"bin" | ||
], | ||
"name": "deps-finder-test", | ||
"src_path": "/home/test/code/centauri/deps-finder-test/src/main.rs" | ||
} | ||
], | ||
"features": {}, | ||
"manifest_path": "/home/test/code/centauri/deps-finder-test/Cargo.toml" | ||
}, | ||
{ | ||
"name": "log", | ||
"version": "0.4.1", | ||
"id": "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"license": "MIT/Apache-2.0", | ||
"license_file": null, | ||
"description": "A lightweight logging facade for Rust\n", | ||
"source": "registry+https://github.com/rust-lang/crates.io-index", | ||
"dependencies": [], | ||
"targets": [ | ||
{ | ||
"kind": [ | ||
"lib" | ||
], | ||
"crate_types": [ | ||
"lib" | ||
], | ||
"name": "log", | ||
"src_path": "/home/test/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/log-0.4.1/src/lib.rs" | ||
}, | ||
{ | ||
"kind": [ | ||
"test" | ||
], | ||
"crate_types": [ | ||
"bin" | ||
], | ||
"name": "filters", | ||
"src_path": "/home/test/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/log-0.4.1/tests/filters.rs" | ||
} | ||
], | ||
"features": {}, | ||
"manifest_path": "/home/test/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/log-0.4.1/Cargo.toml" | ||
}, | ||
{ | ||
"name": "simple_logger", | ||
"version": "0.5.0", | ||
"id": "simple_logger 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"license": "MIT", | ||
"license_file": null, | ||
"description": "A logger that prints all messages with a readable output format", | ||
"source": "registry+https://github.com/rust-lang/crates.io-index", | ||
"dependencies": [ | ||
{ | ||
"name": "log", | ||
"source": "registry+https://github.com/rust-lang/crates.io-index", | ||
"req": "^0.4.1", | ||
"kind": null, | ||
"optional": false, | ||
"uses_default_features": true, | ||
"features": [ | ||
"std" | ||
], | ||
"target": null | ||
} | ||
], | ||
"targets": [ | ||
{ | ||
"kind": [ | ||
"lib" | ||
], | ||
"crate_types": [ | ||
"lib" | ||
], | ||
"name": "simple_logger", | ||
"src_path": "/home/test/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/simple_logger-0.5.0/src/lib.rs" | ||
}, | ||
{ | ||
"kind": [ | ||
"example" | ||
], | ||
"crate_types": [ | ||
"bin" | ||
], | ||
"name": "init", | ||
"src_path": "/home/test/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/simple_logger-0.5.0/examples/init.rs" | ||
}, | ||
{ | ||
"kind": [ | ||
"example" | ||
], | ||
"crate_types": [ | ||
"bin" | ||
], | ||
"name": "init_with_level", | ||
"src_path": "/home/test/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/simple_logger-0.5.0/examples/init_with_level.rs" | ||
} | ||
], | ||
"features": {}, | ||
"manifest_path": "/home/test/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/simple_logger-0.5.0/Cargo.toml" | ||
} | ||
], | ||
"workspace_members": [ | ||
"thing 0.1.0 (path+file:///path/to/thing)" | ||
], | ||
"resolve": { | ||
"nodes": [ | ||
{ | ||
"id": "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"dependencies": [ | ||
"cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" | ||
] | ||
}, | ||
{ | ||
"id": "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"dependencies": [] | ||
}, | ||
{ | ||
"id": "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"dependencies": [] | ||
}, | ||
{ | ||
"id": "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"dependencies": [ | ||
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" | ||
] | ||
}, | ||
{ | ||
"id": "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"dependencies": [ | ||
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" | ||
] | ||
}, | ||
{ | ||
"id": "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"dependencies": [] | ||
}, | ||
{ | ||
"id": "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"dependencies": [] | ||
}, | ||
{ | ||
"id": "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"dependencies": [] | ||
}, | ||
{ | ||
"id": "simple_logger 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"dependencies": [ | ||
"time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" | ||
] | ||
}, | ||
{ | ||
"id": "deps-finder-test 0.1.0 (path+file:///path/to/thing)", | ||
"dependencies": [ | ||
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", | ||
"simple_logger 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" | ||
] | ||
} | ||
], | ||
"root": "deps-finder-test 0.1.0 (path+file:///path/to/thing)" | ||
}, | ||
"target_directory": "/path/to/thing/target", | ||
"version": 1, | ||
"workspace_root": "/path/to/thing" | ||
} |
66 changes: 66 additions & 0 deletions
66
spec/lib/license_finder/package_managers/cargo_package_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'spec_helper' | ||
|
||
module LicenseFinder | ||
describe CargoPackage do | ||
subject do | ||
described_class.new( | ||
'name' => 'time', | ||
'version' => '0.1.39', | ||
'id' => 'time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)', | ||
'license' => 'MIT/Apache-2.0', | ||
'license_file' => nil, | ||
'description' => "Utilities for working with time-related functions in Rust.\n", | ||
'source' => 'registry+https://github.com/rust-lang/crates.io-index', | ||
'dependencies' => [ | ||
{ | ||
'name' => 'libc', | ||
'source' => 'registry+https://github.com/rust-lang/crates.io-index', | ||
'req' => '^0.2.1', | ||
'kind' => nil, | ||
'optional' => false, | ||
'uses_default_features' => true, | ||
'features' => [], | ||
'target' => nil | ||
} | ||
], | ||
'targets' => [ | ||
{ | ||
'kind' => [ | ||
'lib' | ||
], | ||
'crate_types' => [ | ||
'lib' | ||
], | ||
'name' => 'time', | ||
'src_path' => '/home/test/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/time-0.1.39/src/lib.rs' | ||
} | ||
], | ||
'features' => {}, | ||
'manifest_path' => '/home/test/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/time-0.1.39/Cargo.toml' | ||
) | ||
end | ||
|
||
its(:name) { should == 'time' } | ||
its(:version) { should == '0.1.39' } | ||
its(:summary) { should == 'Utilities for working with time-related functions in Rust.' } | ||
its(:homepage) { should eq '' } | ||
its(:groups) { should == [] } | ||
its(:children) { should == ['libc'] } | ||
its(:package_manager) { should eq 'Cargo' } | ||
|
||
describe '#license_names_from_spec' do | ||
let(:package1) { { 'license' => 'MIT' } } | ||
let(:package2) { { 'license' => 'MIT/Apache-2.0' } } | ||
let(:package3) { { 'license' => 'PSF' } } | ||
let(:package4) { { 'licenses' => ['MIT'] } } | ||
|
||
it 'finds the license for all license structures' do | ||
package = CargoPackage.new(package1) | ||
expect(package.license_names_from_spec).to eq ['MIT'] | ||
|
||
package = CargoPackage.new(package2) | ||
expect(package.license_names_from_spec).to eq ['MIT', 'Apache-2.0'] | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.