Skip to content

Commit

Permalink
feat: add clippy function
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed Nov 1, 2023
1 parent d676005 commit e40cf47
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 11 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,24 @@ fluentci run .

## Jobs

| Job | Description |
| ----- | ------------------ |
| build | build your project |
| test | Run your tests |
| Job | Description |
| ------ | ------------------------------- |
| clippy | Run Rust Clippy on your project |
| build | Build your project |
| test | Run your tests |

```graphql
build(src: String!): String
clippy(src: String!): String
test(src: String!): String
```

## Programmatic usage

You can also use this pipeline programmatically:

```ts
import { build, test } from "https://pkg.fluentci.io/rust_pipeline@v0.6.1/mod.ts";
import { build, test } from "https://pkg.fluentci.io/rust_pipeline@v0.6.2/mod.ts";

await test();
await build();
Expand Down
2 changes: 1 addition & 1 deletion ci.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
build,
test,
} from "https://pkg.fluentci.io/rust_pipeline@v0.6.1/mod.ts";
} from "https://pkg.fluentci.io/rust_pipeline@v0.6.2/mod.ts";

await test();
await build();
5 changes: 5 additions & 0 deletions gen/nexus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ export type NexusGenAllTypes = NexusGenRootTypes & NexusGenScalars
export interface NexusGenFieldTypes {
Query: { // field return type
build: string | null; // String
clippy: string | null; // String
test: string | null; // String
}
}

export interface NexusGenFieldTypeNames {
Query: { // field return type name
build: 'String'
clippy: 'String'
test: 'String'
}
}
Expand All @@ -60,6 +62,9 @@ export interface NexusGenArgTypes {
build: { // args
src: string; // String!
}
clippy: { // args
src: string; // String!
}
test: { // args
src: string; // String!
}
Expand Down
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

type Query {
build(src: String!): String
clippy(src: String!): String
test(src: String!): String
}
4 changes: 2 additions & 2 deletions src/dagger/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pipeline from "./pipeline.ts";
import { build, test } from "./jobs.ts";
import { clippy, build, test } from "./jobs.ts";

export { pipeline, build, test };
export { pipeline, clippy, build, test };
65 changes: 63 additions & 2 deletions src/dagger/jobs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
import Client, { connect } from "../../deps.ts";

export enum Job {
clippy = "clippy",
test = "test",
build = "build",
}

export const exclude = ["target", ".git", ".devbox", ".fluentci"];

export const clippy = async (src = ".") => {
await connect(async (client: Client) => {
const context = client.host().directory(src);
const ctr = client
.pipeline(Job.test)
.container()
.from("rust:1.73-bookworm")
.withExec(["apt-get", "update"])
.withExec(["apt-get", "install", "-y", "build-essential", "pkg-config"])
.withExec(["rustup", "component", "add", "clippy"])
.withExec(["cargo", "install", "clippy-sarif", "--version", "0.3.0"])
.withExec(["cargo", "install", "sarif-fmt", "--version", "0.3.0"])
.withDirectory("/app", context, { exclude })
.withWorkdir("/app")
.withMountedCache("/app/target", client.cacheVolume("target"))
.withMountedCache("/root/cargo/registry", client.cacheVolume("registry"))
.withExec([
"sh",
"-c",
"cargo clippy \
--all-features \
--message-format=json | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt",
]);

await ctr
.file("/app/rust-clippy-results.sarif")
.export("./rust-clippy-results.sarif");
await ctr.stdout();
});
return "Done";
};

export const test = async (src = ".", options: string[] = []) => {
await connect(async (client: Client) => {
const context = client.host().directory(src);
Expand All @@ -27,7 +60,12 @@ export const test = async (src = ".", options: string[] = []) => {
return "done";
};

export const build = async (src = ".", options: string[] = []) => {
export const build = async (
src = ".",
packageName?: string,
target = "x86_64-unknown-linux-gnu",
options: string[] = []
) => {
await connect(async (client: Client) => {
const context = client.host().directory(src);
const ctr = client
Expand All @@ -38,7 +76,20 @@ export const build = async (src = ".", options: string[] = []) => {
.withWorkdir("/app")
.withMountedCache("/app/target", client.cacheVolume("target"))
.withMountedCache("/root/cargo/registry", client.cacheVolume("registry"))
.withExec(["cargo", "build", "--release", ...options]);
.withExec(
packageName
? [
"cargo",
"build",
"--release",
"-p",
packageName,
"--target",
target,
...options,
]
: ["cargo", "build", "--release", "--target", target, ...options]
);

const result = await ctr.stdout();

Expand All @@ -49,6 +100,14 @@ export const build = async (src = ".", options: string[] = []) => {

export type JobExec = (src?: string) =>
| Promise<string>
| ((
src?: string,
packageName?: string,
target?: string,
options?: {
ignore: string[];
}
) => Promise<string>)
| ((
src?: string,
options?: {
Expand All @@ -57,11 +116,13 @@ export type JobExec = (src?: string) =>
) => Promise<string>);

export const runnableJobs: Record<Job, JobExec> = {
[Job.clippy]: clippy,
[Job.test]: test,
[Job.build]: build,
};

export const jobDescriptions: Record<Job, string> = {
[Job.clippy]: "Run clippy",
[Job.test]: "Run tests",
[Job.build]: "Build the project",
};
6 changes: 6 additions & 0 deletions src/dagger/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { gql } from "../../deps.ts";

export const clippy = gql`
query clippy($src: String!) {
clippy(src: $src)
}
`;

export const test = gql`
query test($src: String!) {
test(src: $src)
Expand Down
8 changes: 7 additions & 1 deletion src/dagger/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ import {
nonNull,
} from "../../deps.ts";

import { test, build } from "./jobs.ts";
import { clippy, test, build } from "./jobs.ts";

const Query = queryType({
definition(t) {
t.string("clippy", {
args: {
src: nonNull(stringArg()),
},
resolve: async (_root, args, _ctx) => await clippy(args.src),
});
t.string("test", {
args: {
src: nonNull(stringArg()),
Expand Down

0 comments on commit e40cf47

Please sign in to comment.