Skip to content

Commit

Permalink
feat(vibranium::compiler): introduce simple Solcjs strategy
Browse files Browse the repository at this point in the history
With this commit it's possible to use `solcjs` with the compile command:

```
$ vibranium compile --compiler solcjs
```

While this was possible before, it only worked when applying compiler options
manually as there wasn't built-in support:

```
$ vibranium compile --compiler solcjs -- --abi -o file1 file2...
```
  • Loading branch information
0x-r4bbit committed Apr 11, 2019
1 parent fb74857 commit a7cc324
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
18 changes: 12 additions & 6 deletions .azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
- job: 'Test'
strategy:
matrix:
windows-stable:
imageName: 'vs2017-win2016'
rustup_toolchain: stable
windows-beta:
imageName: 'vs2017-win2016'
rustup_toolchain: beta
#windows-stable:
#imageName: 'vs2017-win2016'
#rustup_toolchain: stable
#windows-beta:
#imageName: 'vs2017-win2016'
#rustup_toolchain: beta
mac-stable:
imageName: 'macos-10.13'
rustup_toolchain: stable
Expand All @@ -33,6 +33,12 @@ jobs:
pool:
vmImage: $(imageName)
steps:
- task: NodeTool@0
inputs:
versionSpec: "10.5.0"
displayName: Install Node
- script: npm install -g solc
displayName: Install solcjs
- script: |
curl https://sh.rustup.rs -sSf | sh -s -- -y
echo "##vso[task.setvariable variable=PATH;]$PATH:$HOME/.cargo/bin"
Expand Down
5 changes: 4 additions & 1 deletion cli/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,14 @@ fn it_should_fail_when_compiler_program_fails() -> Result<(), Box<std::error::Er
let mut cmd = Command::main_binary()?;

cmd.arg("compile")
.arg("--compiler")
.arg("solcjs")
.arg("--path")
.arg(&project_path);

cmd.assert()
.failure();
.failure()
.stderr(predicate::str::contains("Must provide a file"));

tmp_dir.close()?;
Ok(())
Expand Down
1 change: 1 addition & 0 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl<'a> Compiler<'a> {

let compiler_strategy = match config.compiler.parse() {
Ok(SupportedCompilers::Solc) => CompilerStrategy::new(Box::new(SolcStrategy::new(strategy_config))),
Ok(SupportedCompilers::SolcJs) => CompilerStrategy::new(Box::new(SolcJsStrategy::new(strategy_config))),
Err(err) => {
if config.compiler_options.is_empty() {
return Err(err)
Expand Down
1 change: 1 addition & 0 deletions src/compiler/strategy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod default;
pub mod solcjs;
pub mod solc;

use std::path::{PathBuf};
Expand Down
43 changes: 43 additions & 0 deletions src/compiler/strategy/solcjs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use super::{Strategy, StrategyConfig};
use std::process::{Command, Child, Stdio};
use glob::glob;

pub const SOLC_JS_COMPILER_BINARY: &str = "solcjs";

pub struct SolcJsStrategy<'a> {
config: StrategyConfig<'a>
}

impl<'a> SolcJsStrategy<'a> {
pub fn new(config: StrategyConfig) -> SolcJsStrategy {
SolcJsStrategy {
config
}
}
}

impl<'a> Strategy for SolcJsStrategy<'a> {

fn execute(&self) -> Result<Child, std::io::Error> {
let mut args: Vec<String> = vec![
"--abi".to_string(),
"-o".to_string(),
self.config.output_path.to_string_lossy().to_string()
];

for pattern in &self.config.smart_contract_sources {
let mut full_pattern = self.config.input_path.clone();
full_pattern.push(&pattern);
for entry in glob(&full_pattern.to_str().unwrap()).unwrap().filter_map(Result::ok) {
args.push(entry.to_string_lossy().to_string());
}
}

Command::new(SOLC_JS_COMPILER_BINARY)
.args(args)
.args(&self.config.compiler_options)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
6 changes: 5 additions & 1 deletion src/compiler/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ use super::strategy;
use std::str::FromStr;
use std::string::ToString;
use strategy::solc::SOLC_COMPILER_BINARY;
use strategy::solcjs::SOLC_JS_COMPILER_BINARY;

pub enum SupportedCompilers {
Solc,

SolcJs,
}

impl FromStr for SupportedCompilers {
type Err = error::CompilerError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
SOLC_COMPILER_BINARY => return Ok(SupportedCompilers::Solc),
SOLC_JS_COMPILER_BINARY => return Ok(SupportedCompilers::SolcJs),
_ => Err(error::CompilerError::UnsupportedStrategy),
}
}
Expand All @@ -23,6 +26,7 @@ impl ToString for SupportedCompilers {
fn to_string(&self) -> String {
match self {
SupportedCompilers::Solc => SOLC_COMPILER_BINARY.to_string(),
SupportedCompilers::SolcJs => SOLC_JS_COMPILER_BINARY.to_string(),
}
}
}
Expand Down

0 comments on commit a7cc324

Please sign in to comment.