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 cicd actions #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * as fs from "https://deno.land/std@0.78.0/fs/mod.ts";
export * as path from "https://deno.land/std@0.78.0/path/mod.ts";
export * as fs from "https://deno.land/std@0.79.0/fs/mod.ts";
export * as path from "https://deno.land/std@0.79.0/path/mod.ts";
export * as docopt from "https://denopkg.com/Eyal-Shalev/[email protected]/src/docopt.ts";
export * as tsdsh from "https://denopkg.com/shah/[email protected]/mod.ts";
export * as stdEncodeYAML from "https://deno.land/[email protected]/encoding/yaml.ts";
4 changes: 2 additions & 2 deletions download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function download(
): Promise<DownlodedFile> {
let file: string;

let dir: string = "";
let dir = "";
// deno-lint-ignore ban-types
let mode: object = {};

Expand Down Expand Up @@ -65,7 +65,7 @@ export async function download(
dir = dir.replace(/\/$/, "");
fs.ensureDirSync(dir);

const fullPath: string = `${dir}/${file}`;
const fullPath = `${dir}/${file}`;
Deno.writeFileSync(fullPath, unit8arr, mode);
return Promise.resolve({ file, dir, fullPath, size });
}
70 changes: 70 additions & 0 deletions github-actions-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
export interface RunsOn {
"runs-on": string;
}

export interface Steps {
steps: ({
name: string;
uses: string;
} | {
name: string;
run: string;
})[];
}

export interface Test {
"runs-on": string;
strategy: { [key: string]: unknown };
steps: { [key: string]: unknown };
}

export interface Matrix {
deno: string[];
}

export interface Strategy {
matrix: Matrix;
}

export interface OnEvent {
push: { branches: string };
pull_request: { branches: string };
}

export interface Jobs {
test: Test;
}

export interface GitHubActionsSettings {
name: string;
on: { [key: string]: unknown };
jobs: { [key: string]: unknown };
}

export function gitHubActionsConfig(
config: Partial<GitHubActionsSettings>,
): Record<string, unknown> {
return {
name: config.name || "Deno",
on: config.on || {
push: { branches: "main" },
pull_request: { branches: "main" },
},
jobs: config.jobs || {
test: {
"runs-on": "ubuntu-latest",
strategy: {
matrix: { deno: ["v1.x", "nightly"] },
},
steps: [
{ name: "Setup repo", uses: "actions/checkout@v2" },
{
name: "Install Deno and execute unit testing",
run: `curl -fsSL https://deno.land/x/install/install.sh | sh &&
export PATH=$PATH:/home/runner/.deno/bin; deno --version; deno info; deno lint --unstable && deno fmt --unstable && deno test -A --unstable;`,
},
],
},
},
};
}
32 changes: 32 additions & 0 deletions gitlab-cicd-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export interface Image {
name: string;
}
export interface DenoTest {
image: Image;
stage: string;
script: string[];
}

export interface GitLabCICDSettings {
stages: string[];
DenoTest: DenoTest;
}

export function gitLabCIConfig(
config: Partial<GitLabCICDSettings>,
): Record<string, unknown> {
return {
stages: config.stages || ["testing"],
DenoTest: config.DenoTest || {
image: { name: "hayd/deno:latest" },
stage: "testing",
script: [
"deno --version",
"deno info",
"deno lint --unstable",
"deno fmt --unstable",
"deno test -A --unstable",
],
},
};
}
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export * from "./tsconfig-settings.ts";
export * from "./node-settings.ts";
export * from "./python-settings.ts";
export * from "./vscode.ts";
export * from "./github-actions-settings.ts";
export * from "./gitlab-cicd-settings.ts";
41 changes: 40 additions & 1 deletion project.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { fs, path } from "./deps.ts";
import { fs, path, stdEncodeYAML as yaml } from "./deps.ts";
import * as dl from "./download.ts";
import * as vscConfig from "./vscode-settings.ts";
import type * as reactVscodeSettings from "./react-settings.ts";
import type { TypeScriptCompilerConfig } from "./tsconfig-settings.ts";
import { NodeESLintSettings, NodePackageConfig } from "./node-settings.ts";
import * as shell from "./shell.ts";
import { gitLabCIConfig } from "./gitlab-cicd-settings.ts";
import { gitHubActionsConfig } from "./github-actions-settings.ts";

export type FsPathOnly = string;
export type AbsoluteFsPath = FsPathOnly;
Expand Down Expand Up @@ -180,9 +182,13 @@ export interface GitWorkTree extends ProjectPath {
readonly gitDir: FsPathOnly;
gitConfig: {
readonly preCommitHookFileName: AbsoluteFsPathAndFileName;
readonly gitHubActionsConfigFile: FsPathAndFileName;
readonly gitLabCICDConfigFile: FsPathAndFileName;
writeGitPreCommitScript: (
gitPrecommitCmd: shell.ShellExecutableScriptDefn,
) => void;
writeGitLabCICDConfig: () => void;
writeGitHubActionConfig: () => void;
};
}

Expand All @@ -206,6 +212,9 @@ export function enrichGitWorkTree(
const workingTreePath = pp.absProjectPath;
const gitTreePath = path.join(workingTreePath, ".git");
const gitCheckFileName = `${gitTreePath}/hooks/pre-commit`;
const cicdConfigFile = `${workingTreePath}/.gitlab-ci.yml`;
const actionsConfigPath = `${workingTreePath}/.github/workflows`;
const actionsConfigFile = `${actionsConfigPath}/github-deno-pipeline.yml`;
if (fs.existsSync(gitTreePath)) {
const result: GitWorkTree = {
...pp,
Expand All @@ -214,11 +223,40 @@ export function enrichGitWorkTree(
gitWorkTree: workingTreePath,
gitConfig: {
preCommitHookFileName: gitCheckFileName,
gitLabCICDConfigFile: cicdConfigFile,
gitHubActionsConfigFile: actionsConfigFile,
writeGitPreCommitScript: (
gitPrecommitCmd: shell.ShellExecutableScriptDefn,
) => {
shell.writeGitPreCommitScript(gitCheckFileName, gitPrecommitCmd);
},
writeGitLabCICDConfig: () => {
if (!fs.existsSync(cicdConfigFile)) {
Deno.writeTextFileSync(
cicdConfigFile,
yaml.stringify(gitLabCIConfig({})),
);
} else {
console.log(
`GitLab CI/CD configuration already exists at ${cicdConfigFile}`,
);
}
},
writeGitHubActionConfig: () => {
if (!fs.existsSync(actionsConfigPath)) {
fs.ensureDirSync(actionsConfigPath);
}
if (!fs.existsSync(actionsConfigFile)) {
Deno.writeTextFileSync(
actionsConfigFile,
yaml.stringify(gitHubActionsConfig({})),
);
} else {
console.log(
`GitHub Actions configuration already exists at ${actionsConfigFile}`,
);
}
},
},
};
return result;
Expand Down Expand Up @@ -777,6 +815,7 @@ export function enrichDenoProjectByVsCodePlugin(
// # To make the libraries back to polyrepos
// "https://denopkg.com/gov-suite/$1/mod.ts"

// deno-lint-ignore require-await
export async function denoRewriteImportsAsMonoRepo(
ctx: { projectHome: string },
depsGlob = "**/*/deps{-test,}.ts",
Expand Down
43 changes: 25 additions & 18 deletions projectctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { pythonGitPrecommitScript } from "./python-settings.ts";

// TODO: find way to automatically update this, e.g. using something like
// git describe --exact-match --abbrev=0
const $VERSION = "v1.0.3";
const $VERSION = "v1.0.5";
const docoptSpec = `
Visual Studio Team Projects Controller ${$VERSION}.

Expand Down Expand Up @@ -39,7 +39,7 @@ Options:
`;

export interface CommandHandler {
(options: cli.DocOptions): Promise<true | void>;
(options: cli.DocOptions): (Promise<true | void>) | (true | void);
}

export function isDryRun(options: cli.DocOptions): boolean {
Expand All @@ -59,9 +59,9 @@ export function acquireProjectPath(options: cli.DocOptions): mod.ProjectPath {
);
}

export async function inspectProjectHandler(
export function inspectProjectHandler(
options: cli.DocOptions,
): Promise<true | void> {
): true | void {
const { inspect } = options;
if (inspect) {
const pp = acquireProjectPath(options);
Expand Down Expand Up @@ -116,20 +116,27 @@ export async function publishProjectHandler(
}
}

export async function denoSetupOrUpgradeProjectHandler(
export function denoSetupOrUpgradeProjectHandler(
options: cli.DocOptions,
): Promise<true | void> {
): true | void {
const { deno, setup, upgrade } = options;
if (deno && (setup || upgrade)) {
const startPP = acquireProjectPath(options);
if (mod.isVsCodeProjectWorkTree(startPP)) {
if (
mod.isVsCodeProjectWorkTree(startPP) && mod.isGitWorkTree(startPP) &&
mod.isDenoProject(startPP)
) {
if (!isDryRun(options)) {
startPP.vsCodeConfig.writeSettings(mod.denoSettings);
startPP.vsCodeConfig.writeExtensions(mod.denoExtensions);
startPP.gitConfig.writeGitLabCICDConfig();
startPP.gitConfig.writeGitHubActionConfig();
}
if (isDryRun || isVerbose(options)) {
if (isDryRun(options) || isVerbose(options)) {
console.log(startPP.vsCodeConfig.settingsFileName);
console.log(startPP.vsCodeConfig.extensionsFileName);
console.log(startPP.gitConfig.gitLabCICDConfigFile);
console.log(startPP.gitConfig.gitHubActionsConfigFile);
}
const upgraded = acquireProjectPath(options);
if (isVerbose(options)) console.dir(upgraded);
Expand Down Expand Up @@ -176,9 +183,9 @@ export async function denoUpdateDependenciesHandler(
}
}

export async function hugoSetupOrUpgradeProjectHandler(
export function hugoSetupOrUpgradeProjectHandler(
options: cli.DocOptions,
): Promise<true | void> {
): true | void {
const { hugo, setup, upgrade } = options;
if (hugo && (setup || upgrade)) {
const startPP = acquireProjectPath(options);
Expand All @@ -205,9 +212,9 @@ export async function hugoSetupOrUpgradeProjectHandler(
}
}

export async function reactSetupOrUpgradeProjectHandler(
export function reactSetupOrUpgradeProjectHandler(
options: cli.DocOptions,
): Promise<true | void> {
): true | void {
const { react, setup, upgrade } = options;
if (react && (setup || upgrade)) {
const startPP = acquireProjectPath(options);
Expand All @@ -234,9 +241,9 @@ export async function reactSetupOrUpgradeProjectHandler(
}
}

export async function nodeSetupOrUpgradeProjectHandler(
export function nodeSetupOrUpgradeProjectHandler(
options: cli.DocOptions,
): Promise<true | void> {
): true | void {
const { node, setup, upgrade } = options;
if (node && (setup || upgrade)) {
const startPP = acquireProjectPath(options);
Expand Down Expand Up @@ -280,9 +287,9 @@ export async function nodeSetupOrUpgradeProjectHandler(
}
}

export async function pythonSetupOrUpgradeProjectHandler(
export function pythonSetupOrUpgradeProjectHandler(
options: cli.DocOptions,
): Promise<true | void> {
): true | void {
const { python, setup, upgrade } = options;
if (python && (setup || upgrade)) {
const startPP = acquireProjectPath(options);
Expand Down Expand Up @@ -316,9 +323,9 @@ export async function pythonSetupOrUpgradeProjectHandler(
}
}

export async function ctlVersionHandler(
export function ctlVersionHandler(
options: cli.DocOptions,
): Promise<true | void> {
): true | void {
const { "--version": version } = options;
if (version) {
console.log($VERSION);
Expand Down
2 changes: 1 addition & 1 deletion vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export async function workspaceFoldersGitCommandHandler(
: [wsFileName.toString()],
}).forEach((ctx) => {
if (prj.isGitWorkTree(ctx.folder)) {
const gitCommand: string =
const gitCommand =
`git --git-dir=${ctx.folder.gitDir} --work-tree=${ctx.folder.gitWorkTree} ${gitCmd}`;
rsCmdOptions = createRunShellCmdOptionsBlockHeader(ctx, rsCmdOptions);
cmdRuns.push(
Expand Down
9 changes: 5 additions & 4 deletions wsctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as mod from "./mod.ts";
// See example in configctl.ts of how to properly organize the CLI so
// that the code works in a CLI or as a library.

const $VERSION = "v1.0.3";
const $VERSION = "v1.0.5";
const docoptSpec = `
Visual Studio Team Workspaces Controller ${$VERSION}.

Expand Down Expand Up @@ -46,9 +46,10 @@ Options:
`;

export interface CommandHandler {
(options: DocOptions): Promise<true | void>;
(options: DocOptions): (Promise<true | void>) | (true | void);
}

// deno-lint-ignore require-await
export async function vscwsInspectFoldersHandler(
options: DocOptions,
): Promise<true | void> {
Expand All @@ -68,9 +69,9 @@ export async function vscwsInspectFoldersHandler(
}
}

export async function setupHandler(
export function setupHandler(
options: DocOptions,
): Promise<true | void> {
): true | void {
const {
setup,
"<workspaces-home-path>": workspacesHomePath,
Expand Down