Skip to content

Commit

Permalink
feat: Use clap v4 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
LekoArts authored Oct 3, 2022
1 parent b3819db commit 75c992e
Show file tree
Hide file tree
Showing 14 changed files with 719 additions and 105 deletions.
51 changes: 39 additions & 12 deletions .github/workflows/build_and_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ jobs:
docker tag ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine builder
build: |
docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/build -w /build builder sh -c "apk add perl && yarn build --target=aarch64-unknown-linux-musl && /aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip *.node"
name: stable - ${{ matrix.settings.target }} - node@16
name: ${{ matrix.settings.target }}
runs-on: ${{ matrix.settings.host }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup node
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: 16
check-latest: true
Expand All @@ -110,17 +110,17 @@ jobs:
with:
command: generate-lockfile
- name: Cache cargo registry
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/.cargo/registry
key: ${{ matrix.settings.target }}-node@16-cargo-registry-trimmed-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/.cargo/git
key: ${{ matrix.settings.target }}-node@16-cargo-index-trimmed-${{ hashFiles('**/Cargo.lock') }}
- name: Cache NPM dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: node_modules
key: npm-cache-${{ matrix.settings.target }}-node@16-${{ hashFiles('yarn.lock') }}
Expand All @@ -139,26 +139,53 @@ jobs:
run: ${{ matrix.settings.build }}
shell: bash
- name: Upload artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: bindings-${{ matrix.settings.target }}
path: ${{ env.APP_NAME }}.*.node
if-no-files-found: error
test:
name: Unit Tests
needs:
- build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 16
check-latest: true
cache: yarn
- name: Cache NPM dependencies
uses: actions/cache@v3
with:
path: node_modules
key: npm-cache-test-linux-x64-gnu-node@16-${{ hashFiles('yarn.lock') }}
- name: Install dependencies
run: yarn install --ignore-scripts --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: bindings-x86_64-unknown-linux-gnu
path: .
- name: Run unit tests
run: yarn test
publish:
name: Publish
runs-on: ubuntu-latest
needs:
- build
- test
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup node
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: 16
check-latest: true
cache: yarn
- name: Cache NPM dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: node_modules
key: npm-cache-ubuntu-latest-${{ hashFiles('yarn.lock') }}
Expand All @@ -167,7 +194,7 @@ jobs:
- name: Install dependencies
run: yarn install --ignore-scripts --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000
- name: Download all artifacts
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
path: artifacts
- name: Move artifacts
Expand Down
10 changes: 0 additions & 10 deletions .npmignore

This file was deleted.

51 changes: 51 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Contributing

## Development

### Build

After `yarn build/npm run build` command, you can see `thanks-contributors.[darwin|win32|linux].node` file in project root. This is the native addon built from [lib.rs](./src/lib.rs).

### CI

With GitHub actions, every commits and pull request will be built and tested automatically in [`node@14`, `@node16`] x [`macOS`, `Linux`, `Windows`] matrix. You will never be afraid of the native addon broken in these platforms.

### Release

Release native package is very difficult in old days. Native packages may ask developers who use its to install `build toolchain` like `gcc/llvm` , `node-gyp` or something more.

With `GitHub actions`, we can easily prebuild `binary` for major platforms. And with `N-API`, we should never afraid of **ABI Compatible**.

The other problem is how to deliver prebuild `binary` to users. Download it in `postinstall` script is a common way which most packages do it right now. The problem of this solution is it introduced many other packages to download binary which has not been used by `runtime codes`. The other problem is some user may not easily download the binary from `GitHub/CDN` if they are behind private network (But in most case, they have a private NPM mirror).

In this package we choose a better way to solve this problem. We release different `npm packages` for different platform. And add it to `optionalDependencies` before release the `Major` package to npm.

`NPM` will choose which native package should download from `registry` automatically. You can see [npm](./npm) dir for details. And you can also run `yarn add @napi-rs/thanks-contributors` to see how it works.

### Develop requirements

- Install latest `Rust`
- Install `Node.js@14+` which fully supported `Node-API`
- Install `[email protected]`

### Test in local

- `yarn`
- `yarn build:debug`
- `node bin.js --help`

### Release package

Ensure you have set you **NPM_TOKEN** in `GitHub` project setting.

In `Settings -> Secrets`, add **NPM_TOKEN** into it.

When you want release package:

```
npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]
git push
```

GitHub actions will do the rest job for you.
24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ version = "0.0.0"
crate-type = ["cdylib"]

[dependencies]
chrono = "0.4.19"
clap = { version = "3.1.2", features = ["derive"] }
dotenv = "0.15.0"
log = "0.4.14"
napi = { version = "2.1.0", features = ["napi8", "serde_json", "async"] }
napi-derive = "2.0.8"
regex = "1.5.4"
reqwest = { version = "0.11.9", features = ["json", "native-tls-vendored"] }
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
stderrlog = "0.5.1"
chrono = "0.4"
clap = { version = "4", features = ["derive", "wrap_help"] }
dotenv = "0.15"
log = "0.4"
napi = { version = "2", features = ["napi8", "serde_json", "async"] }
napi-derive = "2"
regex = "1"
reqwest = { version = "0.11", features = ["json", "native-tls-vendored"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
stderrlog = "0.5"
thiserror = "1.0.30"

[build-dependencies]
napi-build = "1.2.1"
napi-build = "2"

[profile.release]
lto = true
50 changes: 0 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,53 +62,3 @@ If the url can't be found only the name will be printed.
## Caveats
- Getting the PR number only works if you consistently add the number in the commit itself, e.g. in `feat: My cool feature (#123)`. This automatically happens in GitHub's UI if you use squash commits.
## Development
### Build
After `yarn build/npm run build` command, you can see `thanks-contributors.[darwin|win32|linux].node` file in project root. This is the native addon built from [lib.rs](./src/lib.rs).
### CI
With GitHub actions, every commits and pull request will be built and tested automatically in [`node@14`, `@node16`] x [`macOS`, `Linux`, `Windows`] matrix. You will never be afraid of the native addon broken in these platforms.
### Release
Release native package is very difficult in old days. Native packages may ask developers who use its to install `build toolchain` like `gcc/llvm` , `node-gyp` or something more.
With `GitHub actions`, we can easily prebuild `binary` for major platforms. And with `N-API`, we should never afraid of **ABI Compatible**.
The other problem is how to deliver prebuild `binary` to users. Download it in `postinstall` script is a common way which most packages do it right now. The problem of this solution is it introduced many other packages to download binary which has not been used by `runtime codes`. The other problem is some user may not easily download the binary from `GitHub/CDN` if they are behind private network (But in most case, they have a private NPM mirror).
In this package we choose a better way to solve this problem. We release different `npm packages` for different platform. And add it to `optionalDependencies` before release the `Major` package to npm.
`NPM` will choose which native package should download from `registry` automatically. You can see [npm](./npm) dir for details. And you can also run `yarn add @napi-rs/thanks-contributors` to see how it works.
### Develop requirements
- Install latest `Rust`
- Install `Node.js@14+` which fully supported `Node-API`
- Install `[email protected]`
### Test in local
- `yarn`
- `yarn build:debug`
- `node bin.js --help`
### Release package
Ensure you have set you **NPM_TOKEN** in `GitHub` project setting.
In `Settings -> Secrets`, add **NPM_TOKEN** into it.
When you want release package:
```
npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]
git push
```
GitHub actions will do the rest job for you.
46 changes: 46 additions & 0 deletions __tests__/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { invokeCli } from "../vitest.utils"

describe(`cli`, () => {
it(`should show help command`, () => {
const { exitCode, stdout } = invokeCli([`--help`])

expect(stdout).toMatchInlineSnapshot(`
"This little script accesses GitHub's API to get all contributors and their PRs between two distinct
points in the history of commits. This is helpful for changelogs where you'd want to list all
contributions for that release (so e.g. changes between v1 and v1.1).
Usage: @lekoarts/thanks-contributors [OPTIONS] <BASE> <HEAD> [OWNER] [REPO]
Arguments:
<BASE>
Pointer from where to start looking for changes
<HEAD>
Pointer until where to stop looking for changes
[OWNER]
Name of the owner/user/organization of the repository
[default: gatsbyjs]
[REPO]
Name of the repository
[default: gatsby]
Options:
-i, --include-org-members <INCLUDE_ORG_MEMBERS>
Include organization members into the list [default: false]
[possible values: true, false]
-e, --excludes <EXCLUDES>...
List of members to exclude from the list. Usage: -e=member1,member2 [default:
\\"renovate-bot\\", \\"renovate[bot]\\"]
-h, --help
Print help information (use \`-h\` for a summary)"
`)
expect(exitCode).toBe(0)
})
})
6 changes: 0 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,4 @@

/* auto-generated by NAPI-RS */

export class ExternalObject<T> {
readonly '': {
readonly '': unique symbol
[K: symbol]: T
}
}
export function run(args: Array<string>): Promise<void>
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
],
"license": "MIT",
"devDependencies": {
"@napi-rs/cli": "^2.4.4"
"@napi-rs/cli": "^2.11.4",
"execa": "^6.1.0",
"strip-ansi": "^7.0.1",
"vitest": "^0.23.4"
},
"engines": {
"node": ">= 14"
Expand All @@ -35,6 +38,8 @@
"build": "napi build --platform --release",
"build:debug": "napi build --platform",
"prepublishOnly": "napi prepublish -t npm",
"version": "napi version"
"version": "napi version",
"test:watch": "vitest watch",
"test": "vitest run"
}
}
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl From<ThxContribError> for NapiError {
}

impl ThxContribError {
pub fn cli_error<I: clap::IntoApp>(err: clap::Error) -> Self {
pub fn cli_error<I: clap::CommandFactory>(err: clap::Error) -> Self {
let mut app = I::command();
let reason = err.format(&mut app);

Expand Down
23 changes: 15 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ extern crate napi_derive;
use crate::api::{compare_commits, list_members};
use crate::error::ThxContribError;
use crate::utils::{get_current_date, get_pr_link, group_by_author, Entry};
use clap::{FromArgMatches, IntoApp, Parser};
use clap::{CommandFactory, FromArgMatches, Parser};
use dotenv::dotenv;
use log::*;
use napi::bindgen_prelude::{Error as NapiError, Result, Status};
use regex::Regex;
use std::env;
use std::fs;

mod api;
mod error;
mod utils;
pub mod api;
pub mod error;
pub mod utils;

#[allow(dead_code)]
#[napi]
async fn run(args: Vec<String>) -> Result<()> {
dotenv().ok();
Expand Down Expand Up @@ -163,7 +164,8 @@ async fn run(args: Vec<String>) -> Result<()> {
#[clap(
author = "LekoArts",
name = "@lekoarts/thanks-contributors",
about = "This little script accesses GitHub's API to get all contributors and their PRs between two distinct points in the history of commits. This is helpful for changelogs where you'd want to list all contributions for that release (so e.g. changes between v1 and v1.1)."
about = "Generate a list of contributors for a commit range",
long_about = "This little script accesses GitHub's API to get all contributors and their PRs between two distinct points in the history of commits. This is helpful for changelogs where you'd want to list all contributions for that release (so e.g. changes between v1 and v1.1)."
)]
#[clap(no_binary_name = true)]
struct Cli {
Expand All @@ -186,10 +188,15 @@ struct Cli {
#[clap(
short,
long,
multiple_values = true,
takes_value = true,
num_args = 1..,
use_value_delimiter = true,
require_value_delimiter = true
value_delimiter = ',',
)]
excludes: Option<Vec<String>>,
}

#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert()
}
Loading

0 comments on commit 75c992e

Please sign in to comment.