Skip to content

Commit

Permalink
feat(windows) powershell and batch support (fix mac) (#58)
Browse files Browse the repository at this point in the history
* add batch scripting for windows

* add powershell scripts to maskfile

* update tests

* add powershell test to runtime

* update maskfile

* ignore powershell on non windows platforms

* filter powershell, batch and cmd on linux

* fix bitor

* flip `or` to `and`.

* test-action

* test-ci

* Delete rust.yml

* add windows logic

* Update ci.yml

* add if statements

* Update ci.yml

* add github action for windows

* fix if stattements

* fix spelling error

* update powershell script and add cmd/batch tests

* change write-host to write-output

* update maskfile powershell

* Update maskfile.md

Co-Authored-By: Jake Deichert <[email protected]>

* remove unnessecary env calls from powershell

* remove shortcase flags from powershell scripts.

* add mac support for CI

* add macos to build

* trying adding a powershell specific flag

Co-authored-by: Jake Deichert <[email protected]>
  • Loading branch information
tensor-programming and jacobdeichert authored Apr 26, 2020
1 parent 94c0a7b commit 1e99e5c
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 19 deletions.
36 changes: 22 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ on: [push, pull_request]

jobs:
build:
name: Build
runs-on: ubuntu-latest
name: ${{ matrix.platform }}-build
runs-on: ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout master
uses: actions/checkout@master
Expand All @@ -15,8 +19,12 @@ jobs:
run: cargo build --release --frozen

test:
name: Test
runs-on: ubuntu-latest
name: ${{ matrix.platform }}-test
runs-on: ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout master
uses: actions/checkout@master
Expand All @@ -28,16 +36,16 @@ jobs:
run: cargo fetch
- name: Build in test mode
run: cargo build --tests --frozen
- name: Make mask available globally for certain test suites that depend on it
run: cp ./target/debug/mask /usr/share/rust/.cargo/bin
- name: Make mask available globally (windows)
run: copy ./target/debug/mask.exe ~/.cargo/bin/
if: matrix.platform == 'windows-latest'
- name: Make mask available globally (linux)
run: cp ./target/debug/mask ~/.cargo/bin
if: matrix.platform == 'ubuntu-latest'
- name: Make mask available globally (mac)
run: cp ./target/debug/mask ~/.cargo/bin
if: matrix.platform == 'macos-latest'
- name: Run tests
run: cargo test --frozen

format:
name: Format
runs-on: ubuntu-latest
steps:
- name: Checkout master
uses: actions/checkout@master
- name: Verify formatting is correct
run: cargo fmt --all -- --check

61 changes: 60 additions & 1 deletion maskfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ else
fi
~~~

**Note:** On Windows platforms, `mask` falls back to running `powershell` code blocks.

~~~powershell
param (
$maskfile_command = $env:maskfile_command,
$watch = $env:watch
)
$cargo_cmd = "cargo run -- $maskfile_command"
$extra_args = "--exts rs --restart $cargo_cmd"
if ($watch) {
Start-Process watchexec -ArgumentList $extra_args -NoNewWindow -PassThru
} else {
cargo run -- $maskfile_command
}
~~~


## build
Expand All @@ -37,7 +54,9 @@ fi
cargo build --release
~~~


~~~powershell
cargo build --release
~~~

## link

Expand All @@ -47,6 +66,9 @@ cargo build --release
cargo install --force --path .
~~~

~~~powershell
[Diagnostics.Process]::Start("cargo", "install --force --path .").WaitForExit()
~~~


## test
Expand Down Expand Up @@ -78,7 +100,26 @@ fi
echo "Tests passed!"
~~~

~~~powershell
param (
$file = $env:file
)
$extra_args = ""
$verbose = $env:verbose
if ($verbose) {
$extra_args = "-- --nocapture --test-threads=1"
}
Write-Output "Running tests..."
if (!$file) {
cargo test $extra_args
} else {
cargo test --test $file $extra_args
}
Write-Output "Tests passed!"
~~~

## deps

Expand All @@ -92,6 +133,9 @@ echo "Tests passed!"
cargo update
~~~

~~~powershell
cargo update
~~~


## format
Expand All @@ -111,6 +155,17 @@ else
fi
~~~

~~~powershell
param (
$check = $env:check
)
if ($check) {
cargo fmt --all -- --check
} else {
cargo fmt
}
~~~


## lint
Expand All @@ -120,3 +175,7 @@ fi
~~~bash
cargo clippy
~~~

~~~powershell
cargo clippy
~~~
12 changes: 12 additions & 0 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ fn prepare_command(cmd: &Command) -> process::Command {
child.arg("-r").arg(source);
child
}
#[cfg(windows)]
"cmd" | "batch" => {
let mut child = process::Command::new("cmd.exe");
child.arg("/c").arg(source);
child
}
#[cfg(windows)]
"powershell" => {
let mut child = process::Command::new("powershell.exe");
child.arg("-c").arg(source);
child
}
// Any other executor that supports -c (sh, bash, zsh, fish, dash, etc...)
_ => {
let mut child = process::Command::new(executor);
Expand Down
20 changes: 20 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ pub fn build_command_structure(maskfile_contents: String) -> Command {
}
current_command = Command::new(heading_level as u8);
}
#[cfg(not(windows))]
Tag::CodeBlock(lang_code) => {
if lang_code.to_string() != "powershell"
&& lang_code.to_string() != "batch"
&& lang_code.to_string() != "cmd"
{
current_command.script.executor = lang_code.to_string();
}
}
#[cfg(windows)]
Tag::CodeBlock(lang_code) => {
current_command.script.executor = lang_code.to_string();
}
Expand All @@ -52,6 +62,16 @@ pub fn build_command_structure(maskfile_contents: String) -> Command {
Tag::BlockQuote => {
current_command.desc = text.clone();
}
#[cfg(not(windows))]
Tag::CodeBlock(lang_code) => {
if lang_code.to_string() != "powershell"
&& lang_code.to_string() != "batch"
&& lang_code.to_string() != "cmd"
{
current_command.script.source = text.to_string();
}
}
#[cfg(windows)]
Tag::CodeBlock(_) => {
current_command.script.source = text.to_string();
}
Expand Down
73 changes: 73 additions & 0 deletions tests/arguments_and_flags_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ fn positional_arguments() {
~~~bash
echo "Testing $test_case in $file"
~~~
~~~powershell
param (
$test_case = $env:test_case,
$file = $env:file
)
Write-Output "Testing $test_case in $file"
~~~
"#,
);

Expand Down Expand Up @@ -64,6 +73,20 @@ else
echo $PORT
fi
```
```powershell
param (
[string]
[Parameter(Mandatory = $false)]
$port = $env:port
)
if ($env:verbose) {
Write-Output "Starting an http server on PORT: $port"
} else {
Write-Output $port
}
```
"#,
);

Expand Down Expand Up @@ -97,6 +120,16 @@ mod when_entering_negative_numbers {
~~~bash
echo $(($a + $b))
~~~
~~~powershell
param (
[int]$a = $env:a,
[int]$b = $env:b
)
$sum = "$($a + $b)"
Write-Output $sum
~~~
"#,
);

Expand Down Expand Up @@ -124,6 +157,16 @@ echo $(($a + $b))
~~~bash
echo $(($a + $b))
~~~
~~~powershell
param (
[int]$a = $env:a,
[int]$b = $env:b
)
$sum = "$($a + $b)"
Write-Output $sum
~~~
"#,
);

Expand Down Expand Up @@ -152,6 +195,13 @@ mod numerical_option_flag {
~~~bash
echo "Value: $val"
~~~
~~~powershell
param (
$in = $env:val
)
Write-Output "Value: $in"
~~~
"#,
);

Expand All @@ -176,6 +226,14 @@ echo "Value: $val"
~~~bash
echo "Value: $val"
~~~
~~~powershell
param (
[int]$in = $env:val
)
Write-Output "Value: $in"
~~~
"#,
);

Expand All @@ -200,6 +258,13 @@ echo "Value: $val"
~~~bash
echo "Value: $val"
~~~
~~~powershell
param (
[Double]$in = $env:val
)
Write-Output "Value: $in"
~~~
"#,
);

Expand All @@ -224,6 +289,10 @@ echo "Value: $val"
~~~bash
echo "This shouldn't render"
~~~
~~~powershell
Write-Output "This shouldn't render"
~~~
"#,
);

Expand Down Expand Up @@ -251,6 +320,10 @@ echo "This shouldn't render"
~~~bash
echo "No arg this time"
~~~
~~~powershell
Write-Output "No arg this time"
~~~
"#,
);

Expand Down
Loading

0 comments on commit 1e99e5c

Please sign in to comment.