Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add after directive #6

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,18 @@ jobs:
- name: Set git to not convert line endings
run: |
git config --global core.autocrlf false
- uses: actions/checkout@v3
- name: Setup Rust
uses: ATiltedTree/setup-rust@v1
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
rust-version: stable
components: clippy
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: Swatinem/rust-cache@v2
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Show Compiler Version
run: rustc --version --verbose
- name: Clippy
run: cargo clippy --all-targets --all-features -- -D warnings
- run: cargo fmt --check
- run: cargo clippy --all-targets --all-features -- -D warnings
- name: Test
run: cargo test --verbose
- name: Make sure clean can run
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 0.2.2
- New `after` directive to specify dependency explicitly

## 0.2.1
- Temp files are no longer re-written if they are already up-to-date in both verify and build mode
- New flag `--needed/-N` and corresponding mode `InMemoryBuild` that stores the fresh output in memory and only writes the file if different
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "txtpp"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
description = "A simple-to-use general purpose preprocessor for text files."
repository = "https://github.com/Pistonite/txtpp"
Expand Down
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ readme:

# Pre-commit checks
check: && readme clean
cargo clippy --all-targets --all-features -- -D warnings
cargo clippy --all-targets --all-features -- -D warnings -D clippy::todo
cargo fmt
cargo doc
cargo test
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ This will execute the command `echo "hello world"`. The `-` in the last line in
## Execution
The directives are executed immediately after they are parsed. They may produce an output to be included in the output file and/or have side effects such as creating a temporary file.

If the directive has output (like `include` and `run`), it will be formated as:
If the directive has output (like `include` and `run`), it will be formatted as:
- Every line in the output will be prepended with `{WHITESPACES}`, so that the indentation is consistent
```
// TXTPP#run echo 1; echo 2
Expand All @@ -159,7 +159,7 @@ If the directive has output (like `include` and `run`), it will be formated as:

```

Note that normally, you will not be able to connect a directive output to the previous line, since directives always start on its own line. However, you can use `tag` (see [below](#tag-directive)) to achieve this. If there is currently an active `tag` directive that is listening for output, the output will be sent to the tag instead of the output file, without the indentation. and the directive will produce no output.
Note that normally, you will not be able to connect a directive output to the previous line, since directives always start on its own line. However, you can use `tag` (see [below](#tag-directive)) to achieve this. If there is currently an active `tag` directive that is listening for output, the output will be sent to the tag instead of the output file, without the indentation, and the directive will produce no output.

# Directive Specification
This section contains detailed specification of each directive.
Expand All @@ -176,7 +176,20 @@ Single-line only. The argument is `FILE_PATH`
```
TXTPP#include foo.txt
```
## After Directive
#### USAGE
This directive is to explicitly specify dependency.
#### ARGUMENTS
Single-line only. The argument is `FILE_PATH`
#### BEHAVIOR
This directive behaves exactly like `include`, except it only changes the dependency structure and doesn't affect the output.

This can be useful if there are implicit dependencies. For example, a `run` directive could execute a command that depends on a txtpp output.
#### EXAMPLE
```
TXTPP#after foo.txt
TXTPP#run ./my-script.sh foo.txt
```
## Run Directive
#### USAGE
This directive is used to run a command and include the output of the command into the current file.
Expand Down
18 changes: 16 additions & 2 deletions docs/README.md.txtpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ This will execute the command `echo "hello world"`. The `-` in the last line in
## Execution
The directives are executed immediately after they are parsed. They may produce an output to be included in the output file and/or have side effects such as creating a temporary file.

If the directive has output (like `include` and `run`), it will be formated as:
If the directive has output (like `include` and `run`), it will be formatted as:
- Every line in the output will be prepended with `{WHITESPACES}`, so that the indentation is consistent
```
TXTPP#include run1.txtpp
Expand All @@ -149,7 +149,7 @@ If the directive has output (like `include` and `run`), it will be formated as:

```

Note that normally, you will not be able to connect a directive output to the previous line, since directives always start on its own line. However, you can use `tag` (see [below](#tag-directive)) to achieve this. If there is currently an active `tag` directive that is listening for output, the output will be sent to the tag instead of the output file, without the indentation. and the directive will produce no output.
Note that normally, you will not be able to connect a directive output to the previous line, since directives always start on its own line. However, you can use `tag` (see [below](#tag-directive)) to achieve this. If there is currently an active `tag` directive that is listening for output, the output will be sent to the tag instead of the output file, without the indentation, and the directive will produce no output.

# Directive Specification
This section contains detailed specification of each directive.
Expand All @@ -167,6 +167,20 @@ Single-line only. The argument is `FILE_PATH`
++TXTPP#include foo.txt
++```

## After Directive
#### USAGE
This directive is to explicitly specify dependency.
#### ARGUMENTS
Single-line only. The argument is `FILE_PATH`
#### BEHAVIOR
This directive behaves exactly like `include`, except it only changes the dependency structure and doesn't affect the output.

This can be useful if there are implicit dependencies. For example, a `run` directive could execute a command that depends on a txtpp output.
#### EXAMPLE
++TXTPP#write ```
++TXTPP#after foo.txt
++TXTPP#run ./my-script.sh foo.txt
++```

## Run Directive
#### USAGE
Expand Down
14 changes: 14 additions & 0 deletions src/core/execute/pp/directive/directive_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,18 @@ mod ut {

assert_eq!(expected, actual);
}

#[test]
fn test_detect_after() {
let line = " random TXTPP#after stuff\t\t";
let expected = Some(Directive::new(
" ",
"random ",
DirectiveType::After,
vec!["stuff".to_string()],
));
let actual = Directive::detect_from(line);

assert_eq!(expected, actual);
}
}
9 changes: 8 additions & 1 deletion src/core/execute/pp/directive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub enum DirectiveType {
Empty,
/// Include directive, argument is path to a file
Include,
/// After directive, argument is path to a file
After,
/// Run directive, argument is a command
Run,
/// Tag directive, argument is a string
Expand All @@ -81,6 +83,7 @@ impl TryFrom<&str> for DirectiveType {
"tag" => Ok(DirectiveType::Tag),
"temp" => Ok(DirectiveType::Temp),
"write" => Ok(DirectiveType::Write),
"after" => Ok(DirectiveType::After),
_ => Err(()),
}
}
Expand All @@ -89,7 +92,10 @@ impl TryFrom<&str> for DirectiveType {
impl DirectiveType {
/// Does directive support multi-line arguments
pub fn supports_multi_line(&self) -> bool {
!matches!(self, DirectiveType::Include | DirectiveType::Tag)
!matches!(
self,
DirectiveType::After | DirectiveType::Include | DirectiveType::Tag
)
}
}

Expand All @@ -98,6 +104,7 @@ impl Display for DirectiveType {
match self {
DirectiveType::Empty => write!(f, ""),
DirectiveType::Include => write!(f, "include"),
DirectiveType::After => write!(f, "after"),
DirectiveType::Run => write!(f, "run"),
DirectiveType::Tag => write!(f, "tag"),
DirectiveType::Temp => write!(f, "temp"),
Expand Down
7 changes: 5 additions & 2 deletions src/core/execute/pp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl<'a> Pp<'a> {
};

let raw_output = match d.directive_type {
DirectiveType::Empty => {
DirectiveType::Empty | DirectiveType::After => {
// do nothing (consume the line)
None
}
Expand Down Expand Up @@ -286,7 +286,10 @@ impl<'a> Pp<'a> {
// never collect deps in execute mode
return Ok(Some(d));
}
if let DirectiveType::Include = d.directive_type {
if matches!(
d.directive_type,
DirectiveType::Include | DirectiveType::After
) {
let arg = d.args.first().cloned().unwrap_or_default();
let include_path = PathBuf::from(&arg);
// We use join instead of share_base because the dependency might not exist
Expand Down
3 changes: 3 additions & 0 deletions tests/examples/after/example.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
In this example, the run directive executes a command that depends on foo. The after directive is used to make sure foo is produced before this file
hello

3 changes: 3 additions & 0 deletions tests/examples/after/example.txtpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
In this example, the run directive executes a command that depends on foo. The after directive is used to make sure foo is produced before this file
TXTPP#after foo
-TXTPP#run cat foo
1 change: 1 addition & 0 deletions tests/examples/after/foo.txtpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
5 changes: 5 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,8 @@ testit!(tests__examples__temp__no_rewrite, |env| {
assert_eq!(modified, modified4); // temp file is always checked before re-written
assert_ne!(modified_out, modified_out4);
});

testit!(tests__examples__after, |env| {
assert!(env.run().is_ok());
env.assert_file_eq("example", "example.expected");
});