diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..b89ef4a --- /dev/null +++ b/.gitattributes @@ -0,0 +1,9 @@ +# The same bytes on every platform: the tests compare the layouts of test/models with +# test/expected byte for byte, and the scripts run in bash. +*.mo text eol=lf +*.rs text eol=lf +*.c text eol=lf +*.sh text eol=lf +*.md text eol=lf +*.svg text eol=lf +*.png binary diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e02e00a..209f67e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,7 +1,7 @@ # Runs the checks and builds mocoal in two forms: the standalone program and the shared library. # Both are tested and uploaded as artifacts. A push or a pull request builds for Linux x86_64; a # run by hand (Actions > Build > Run workflow) also builds for macOS (Apple silicon) and Windows -# x86_64. release.yml calls it for all three. +# x86_64. release.yml calls it for all three, with the commit to release. name: Build on: @@ -15,6 +15,10 @@ on: description: Build for macOS and Windows too. type: boolean default: true + ref: + description: The commit to build (empty for the one of the event). + type: string + default: "" permissions: contents: read @@ -47,6 +51,8 @@ jobs: shell: bash steps: - uses: actions/checkout@v4 + with: + ref: ${{ inputs.ref }} - name: Install Rust id: rust diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6b8d23d..f8a2671 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,48 +1,95 @@ -# Publishes a release for a tag vX.Y.Z, where X.Y.Z is the version in Cargo.toml. It attaches +# Publishes a release of mocoal. Only by hand: Actions > Release > Run workflow; a pushed tag does +# not start it. Inputs: +# - commit: the commit to release, on master. Empty: the head of master. +# - publish: make the tag and the release. Off (the default): a dry run, everything but those two. +# The version X.Y.Z comes from Cargo.toml of that commit; the job makes the tag vX.Y.Z on it, and +# refuses a version whose tag exists. The release has # - the program and the shared library for Linux x86_64, macOS (Apple silicon) and Windows x86_64, # built and tested by build.yml: 6 archives; -# - the complete source, mocoal-rs-X.Y.Z-src.tar.gz: the tagged tree, the parser crates of +# - the complete source, mocoal-rs-X.Y.Z-src.tar.gz: the tree of the commit, the parser crates of # OpenModelica (scripts/fetch-omc.sh) and the crates.io dependencies (cargo vendor). It builds # without network access, and is tested so before the release; # - SHA256SUMS of all of them. # -# git tag v0.1.0 && git push origin v0.1.0 -# -# A pull request that changes this file, or a run by hand, is a dry run: everything but the release. +# A pull request that changes this file is a dry run of the pull request. name: Release on: - push: - tags: ["v*"] pull_request: paths: [".github/workflows/release.yml"] workflow_dispatch: + inputs: + commit: + description: Commit on master to release (empty for the head of master) + type: string + default: "" + publish: + description: Make the tag and the release (off for a dry run) + type: boolean + default: false + +# One release run at a time. +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: false permissions: contents: read jobs: check: - name: Tag and version + name: Commit, version and tag runs-on: ubuntu-22.04 + outputs: + sha: ${{ steps.check.outputs.sha }} + version: ${{ steps.check.outputs.version }} + tag: ${{ steps.check.outputs.tag }} steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - - name: The tag is v and the version in Cargo.toml - if: github.ref_type == 'tag' + - name: Commit, version and tag + id: check shell: bash + env: + COMMIT: ${{ inputs.commit }} + PUBLISH: ${{ inputs.publish }} run: | - version=$(sed -n 's/^version = "\(.*\)"$/\1/p' Cargo.toml | head -1) - if [ "$GITHUB_REF_NAME" != "v$version" ]; then - echo "::error::tag $GITHUB_REF_NAME, but Cargo.toml has version $version" - exit 1 + if [ "$GITHUB_EVENT_NAME" = pull_request ]; then + sha=$(git rev-parse HEAD) + else + if ! sha=$(git rev-parse --verify --quiet "${COMMIT:-origin/master}^{commit}"); then + echo "::error::no such commit: $COMMIT" + exit 1 + fi + if ! git merge-base --is-ancestor "$sha" origin/master; then + echo "::error::$sha is not on master" + exit 1 + fi fi + version=$(git show "$sha:Cargo.toml" | sed -n 's/^version = "\(.*\)"$/\1/p' | head -1) + tag="v$version" + if git ls-remote --exit-code --tags origin "refs/tags/$tag" > /dev/null; then + if [ "$PUBLISH" = true ]; then + echo "::error::the tag $tag exists: change the version in Cargo.toml" + exit 1 + fi + echo "::warning::the tag $tag exists" + fi + echo "commit $sha, version $version, tag $tag, publish ${PUBLISH:-false}" + { + echo "sha=$sha" + echo "version=$version" + echo "tag=$tag" + } >> "$GITHUB_OUTPUT" build: needs: check uses: ./.github/workflows/build.yml with: all_platforms: true + ref: ${{ needs.check.outputs.sha }} sources: name: Source archive @@ -53,11 +100,14 @@ jobs: shell: bash steps: - uses: actions/checkout@v4 + with: + ref: ${{ needs.check.outputs.sha }} - name: Archive + env: + VERSION: ${{ needs.check.outputs.version }} run: | - version=$(sed -n 's/^version = "\(.*\)"$/\1/p' Cargo.toml | head -1) - name="mocoal-rs-$version" + name="mocoal-rs-$VERSION" tree="$RUNNER_TEMP/src/$name" mkdir -p "$tree" dist git archive HEAD | tar -x -C "$tree" @@ -83,8 +133,8 @@ jobs: release: name: GitHub release - needs: [build, sources] - if: github.ref_type == 'tag' + needs: [check, build, sources] + if: github.event_name == 'workflow_dispatch' && inputs.publish runs-on: ubuntu-22.04 permissions: contents: write @@ -102,13 +152,16 @@ jobs: sha256sum -- * > SHA256SUMS cat SHA256SUMS - - name: Release + # Makes the tag on the commit, then the release. + - name: Tag and release shell: bash env: GH_TOKEN: ${{ github.token }} + SHA: ${{ needs.check.outputs.sha }} + TAG: ${{ needs.check.outputs.tag }} run: >- - gh release create "$GITHUB_REF_NAME" dist/* + gh release create "$TAG" dist/* --repo "$GITHUB_REPOSITORY" - --title "mocoal $GITHUB_REF_NAME" + --target "$SHA" + --title "mocoal $TAG" --generate-notes - --verify-tag diff --git a/README.md b/README.md index acd1390..176d861 100644 --- a/README.md +++ b/README.md @@ -254,29 +254,27 @@ arm64 and Windows x86_64 on a run by hand (Actions > Build > Run workflow) and f ## Release -```bash -git tag v0.1.0 -``` +Only by hand: Actions > Release > Run workflow +([.github/workflows/release.yml](.github/workflows/release.yml)), with -```bash -git push origin v0.1.0 -``` +- `commit`: the commit to release, on master. Empty: the head of master. +- `publish`: make the tag and the release. Off (the default): a dry run, everything but those two. -The tag is `v` and the version in `Cargo.toml`. The workflow -[.github/workflows/release.yml](.github/workflows/release.yml) checks that, and makes a GitHub -release with: +The version comes from `Cargo.toml` of that commit. The workflow builds and tests that commit, +makes the tag `v` on it and a GitHub release with: - `mocoal-` and `libmocoal-` for Linux x86_64, macOS arm64 and Windows x86_64 (`.tar.gz`, `.zip` on Windows): the program or the library with `README.md` and the licence files, built and tested as by the Build workflow; -- `mocoal-rs--src.tar.gz`: the complete source. The tagged tree, the parser crates of - OpenModelica and the crates.io dependencies (`cargo vendor`): `cargo build --release --offline` +- `mocoal-rs--src.tar.gz`: the complete source. The tree of the commit, the parser crates + of OpenModelica and the crates.io dependencies (`cargo vendor`): `cargo build --release --offline` builds it without network access (8 MB). GitHub's own "Source code" archives lack the parser crates; - `SHA256SUMS` of all of them. -A pull request that changes `release.yml`, or a run by hand (Actions > Release > Run workflow), -does all of this but the release. +A version whose tag exists is refused: change the version in `Cargo.toml` first. A pushed tag +starts no release. A pull request that changes `release.yml` is a dry run. Nothing is published +to crates.io (`publish = false`). ## Limitations diff --git a/src/source/tokens.rs b/src/source/tokens.rs index f35a0ee..fd53940 100644 --- a/src/source/tokens.rs +++ b/src/source/tokens.rs @@ -131,6 +131,15 @@ fn token_length(text: &str, at: usize, kind: &TokenKind) -> usize { } impl Source { + /// The line ending of the text, for the lines the edits add: "\r\n" when its first line ends so, + /// else "\n". + pub fn newline(&self) -> &'static str { + match self.text.find('\n') { + Some(i) if i > 0 && self.text.as_bytes()[i - 1] == b'\r' => "\r\n", + _ => "\n", + } + } + /// Tokenize `text`. The error is the lexer's message. pub fn new(text: &str, file_name: &str) -> Result { let tokens = lexer::lex(text, Grammar::Modelica3).map_err(|e| e.to_string())?; diff --git a/src/tests.rs b/src/tests.rs index 086a407..5a6367c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -18,9 +18,10 @@ //! Each case also checks the laid-out diagram of every class: no two components overlap, every //! line starts and ends at its ports, orthogonal lines are axis-aligned and pass through no //! component, and a second layout of the result changes nothing. With `keep`, every `Placement` -//! of the input is in the result as it was. +//! of the input is in the result as it was. The same model with CRLF line endings gives the same +//! result with CRLF line endings. -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use crate::api::{LayoutResult, layout_diagrams}; use crate::layout::diagram::{Diagram, Node, Point}; @@ -118,6 +119,11 @@ const CASES: &[Case] = &[ /// Components that keep a place given elsewhere, so the class does not place them: (class, how many). const NOT_PLACED: &[(&str, usize)] = &[("Features.Derived", 1)]; +/// The text of a file with LF line endings. +fn read_lf(path: &Path) -> Option { + std::fs::read_to_string(path).ok().map(|t| t.replace("\r\n", "\n")) +} + fn root() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test") } @@ -146,12 +152,13 @@ fn layouts() { let mut failed = Vec::new(); for case in CASES { let opts = options(case); - let source = std::fs::read_to_string(root().join("models").join(case.model)).unwrap(); + // LF, however git checked the files out. + let source = read_lf(&root().join("models").join(case.model)).unwrap(); let (result, diagrams) = layout(case, &source, &opts); let path = root().join("expected").join(case.expected); if bless { std::fs::write(&path, &result.text).unwrap(); - } else if std::fs::read_to_string(&path).ok().as_deref() != Some(result.text.as_str()) { + } else if read_lf(&path).as_deref() != Some(result.text.as_str()) { failed.push(format!("{}: differs from {}", case.expected, path.display())); } let checks = check_report(&result, &opts).and_then(|()| { @@ -169,6 +176,13 @@ fn layouts() { { failed.push(format!("{}: --keep changes `{}`", case.expected, line.trim())); } + let (crlf, _) = layout(case, &source.replace('\n', "\r\n"), &opts); + if crlf.text != result.text.replace('\n', "\r\n") { + failed.push(format!( + "{}: a CRLF input gives other lines or line endings", + case.expected + )); + } let (again, _) = layout(case, &result.text, &opts); if again.changed { failed.push(format!("{}: a second layout changes the result", case.expected)); diff --git a/src/write/annotate.rs b/src/write/annotate.rs index 693219e..24daf51 100644 --- a/src/write/annotate.rs +++ b/src/write/annotate.rs @@ -275,7 +275,7 @@ pub fn annotate_extent(edits: &mut Vec, src: &Source, d: &mut Diagram, war let text = format!("annotation(Diagram(coordinateSystem({leaf})));"); if line_first == 0 || bytes[line_first - 1] == b'\n' { let indent = &src.text[line_first..at]; - edits.push(insertion(at, format!(" {text}\n{indent}"))); + edits.push(insertion(at, format!(" {text}{}{indent}", src.newline()))); } else { edits.push(insertion(at, format!("{text} "))); } diff --git a/src/write/graphics.rs b/src/write/graphics.rs index 756e56d..8eee7a8 100644 --- a/src/write/graphics.rs +++ b/src/write/graphics.rs @@ -387,7 +387,7 @@ pub fn annotate_graphics(edits: &mut Vec, src: &Source, d: &mut Diagram, o edits.push(Edit { start: src.start(lbrace), stop: src.stop(rbrace), - text: format!("{{{}}}", out.join(",\n ")), + text: format!("{{{}}}", out.join(&format!(",{} ", src.newline()))), }); } }