Skip to content

Commit

Permalink
chore: repo overall update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Nov 19, 2024
1 parent a2aa285 commit 285f6f5
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 7 deletions.
7 changes: 6 additions & 1 deletion plugins/babel-plugin-canyon/features/add.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function add (a,b) {
return a+b
if (a>b){
return a-b
}
return a+b
}

add(1, 2);
4 changes: 2 additions & 2 deletions plugins/babel-plugin-canyon/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "babel-plugin-canyon",
"version": "1.8.78-beta.26",
"version": "1.8.78-beta.27",
"description": "",
"scripts": {
"release": "babel src --extensions \".ts\" --out-dir lib",
"pretest": "npm run release",
"test": "babel features --config-file ./my-custom-babel-config.js --out-dir dist",
"prepublishOnly": "npm run release",
"prepublishOnly": "npm run test",
"format": "biome check --write --unsafe ./src",
"preinstall": "npm run release"
},
Expand Down
11 changes: 11 additions & 0 deletions plugins/babel-plugin-canyon/src/ci_providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IProvider } from '../types'

import * as providerGitHubactions from './provider_githubactions'
import * as providerGitLabci from './provider_gitlabci'

const providerList: IProvider[] = [
providerGitHubactions,
providerGitLabci,
]

export default providerList
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { IServiceParams, UploaderEnvs, UploaderInputs } from '../types'


export function detect(envs: UploaderEnvs): boolean {
return Boolean(envs.GITHUB_ACTIONS)
}

function _getBranch(inputs: UploaderInputs): string {
const { args, envs } = inputs
const branchRegex = /refs\/heads\/(.*)/
const branchMatches = branchRegex.exec(envs.GITHUB_REF || '')
let branch
if (branchMatches) {
branch = branchMatches[1]
}

if (envs.GITHUB_HEAD_REF && envs.GITHUB_HEAD_REF !== '') {
branch = envs.GITHUB_HEAD_REF
}
return args.branch || branch || ''
}

function _getJob(envs: UploaderEnvs): string {
return (envs.GITHUB_WORKFLOW || '')
}

function _getService(): string {
return 'github-actions'
}

export function getServiceName(): string {
return 'GitHub Actions'
}

function _getSHA(inputs: UploaderInputs): string {
const { args, envs } = inputs
return args.sha || envs.GITHUB_SHA
}

function _getSlug(inputs: UploaderInputs): string {
const { args, envs } = inputs
if (args.slug !== '') return args.slug
return envs.GITHUB_REPOSITORY || ''
}

export function getServiceParams(inputs: UploaderInputs): IServiceParams {
return {
branch: _getBranch(inputs),
// build: _getBuild(inputs),
// buildURL: await _getBuildURL(inputs),
commit: _getSHA(inputs),
// job: _getJob(inputs.envs),
// pr: _getPR(inputs),
service: _getService(),
slug: _getSlug(inputs),
}
}

export function getEnvVarNames(): string[] {
return [
'GITHUB_ACTION',
'GITHUB_HEAD_REF',
'GITHUB_REF',
'GITHUB_REPOSITORY',
'GITHUB_RUN_ID',
'GITHUB_SERVER_URL',
'GITHUB_SHA',
'GITHUB_WORKFLOW',
]
}
83 changes: 83 additions & 0 deletions plugins/babel-plugin-canyon/src/ci_providers/provider_gitlabci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { IServiceParams, UploaderEnvs, UploaderInputs } from '../types'

// import { parseSlugFromRemoteAddr } from '../helpers/git'

export function detect(envs: UploaderEnvs): boolean {
return Boolean(envs.GITLAB_CI)
}

function _getBuild(inputs: UploaderInputs): string {
const { args, envs } = inputs
return args.build || envs.CI_BUILD_ID || envs.CI_JOB_ID || ''
}

function _getBuildURL(): string {
return ''
}

function _getBranch(inputs: UploaderInputs): string {
const { args, envs } = inputs
return args.branch || envs.CI_BUILD_REF_NAME || envs.CI_COMMIT_REF_NAME || ''
}

function _getJob(): string {
return ''
}

function _getPR(inputs: UploaderInputs): string {
const { args } = inputs
return args.pr || ''
}

function _getService(): string {
return 'gitlab'
}

export function getServiceName(): string {
return 'GitLab CI'
}

function _getSHA(inputs: UploaderInputs): string {
const { args, envs } = inputs
return args.sha || envs.CI_MERGE_REQUEST_SOURCE_BRANCH_SHA || envs.CI_BUILD_REF || envs.CI_COMMIT_SHA || ''
}

function _getSlug(inputs: UploaderInputs): string {
const { args, envs } = inputs
if (args.slug !== '') return args.slug
const remoteAddr = envs.CI_BUILD_REPO || envs.CI_REPOSITORY_URL || ''
return (
envs.CI_PROJECT_PATH ||
// parseSlugFromRemoteAddr(remoteAddr) ||
''
)
}

export function getServiceParams(inputs: UploaderInputs): IServiceParams {
return {
branch: _getBranch(inputs),
// build: _getBuild(inputs),
// buildURL: _getBuildURL(),
commit: _getSHA(inputs),
// job: _getJob(),
// pr: _getPR(inputs),
service: _getService(),
slug: _getSlug(inputs),
}
}

export function getEnvVarNames(): string[] {
return [
'CI_BUILD_ID',
'CI_BUILD_REF',
'CI_BUILD_REF_NAME',
'CI_BUILD_REPO',
'CI_COMMIT_REF_NAME',
'CI_COMMIT_SHA',
'CI_JOB_ID',
'CI_MERGE_REQUEST_SOURCE_BRANCH_SHA',
'CI_PROJECT_PATH',
'CI_REPOSITORY_URL',
'GITLAB_CI',
]
}
23 changes: 23 additions & 0 deletions plugins/babel-plugin-canyon/src/helpers/extract-coverage-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function extractCoverageData(scriptContent) {
const reg0 = /var\s+coverageData\s*=\s*({[\s\S]*?});/;
const reg1 = /var\s+(\w+)\s*=\s*function\s*\(\)\s*\{([\s\S]*?)\}\(\);/
try {
// 可能性一
const match0 = reg0.exec(scriptContent);
if (match0) {
const objectString = match0[1];
return new Function('return ' + objectString)();
}
// 可能性二
const match1 = reg1.exec(scriptContent);
if (match1) {
const functionBody = match1[2];
const func = new Function(functionBody + 'return coverageData;');
const result = func();
return result;
}
} catch (e) {
return null;
}
return null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from 'fs';
import path from 'path'
import {extractCoverageData} from "./extract-coverage-data";

export const generateInitialCoverage = (paramsPath) => {
const initialCoverageDataForTheCurrentFile = extractCoverageData(paramsPath)
const filePath = './.canyon_output/coverage-final.json';
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {recursive: true});
}
// 防止返回的数据为空
if (initialCoverageDataForTheCurrentFile && initialCoverageDataForTheCurrentFile.path) {
fs.writeFileSync(`./.canyon_output/coverage-map-${Math.random()}.json`, JSON.stringify({
[initialCoverageDataForTheCurrentFile.path]: initialCoverageDataForTheCurrentFile
}, null, 2), 'utf-8');
}
return initialCoverageDataForTheCurrentFile;
}
16 changes: 14 additions & 2 deletions plugins/babel-plugin-canyon/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@ import generate from "@babel/generator";
import { declare } from "@babel/helper-plugin-utils";
import packageJson from "../package.json";
import {visitorProgramExit} from "./visitor-program-exit";
import providers from './ci_providers'

export default declare((api, config) => {
api.assertVersion(7);
return {
visitor: {
Program: {
exit: (path) => {
console.log("Babel Plugin Canyon")
// visitorProgramExit(path)
for (const provider of providers) {
if (provider.detect(process.env)) {
const con = provider.getServiceParams({
envs: process.env,
// @ts-ignore
args: {}
})
console.log(con)
}
}
// throw new Error(`Unable to detect provider.`)
// console.log("Babel Plugin Canyon")
visitorProgramExit(api,path)
}
},
},
Expand Down
91 changes: 91 additions & 0 deletions plugins/babel-plugin-canyon/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// import { Dispatcher, ProxyAgent } from "undici";

export interface UploaderArgs {
branch?: string // Specify the branch manually
build?: string // Specify the build number manually
changelog?: string // Displays the changelog and exits
clean?: string // Move discovered coverage reports to the trash
dir?: string // Directory to search for coverage reports.
dryRun?: string // Don't upload files to Codecov
env?: string // Specify environment variables to be included with this build
feature?: string // Toggle features
file?: string | string[] // Target file(s) to upload
flags: string | string[] // Flag the upload to group coverage metrics
fullReport?: string // Specify the path to a previously uploaded Codecov report
gcov?: string // Run with gcov support
gcovArgs?: string | string[] // Extra arguments to pass to gcov
gcovIgnore?: string | string[] // Paths to ignore during gcov gathering
gcovInclude?: string | string[] // Paths to include during gcov gathering
gcovExecutable?: string // gcov executable to run.
name?: string // Custom defined name of the upload. Visible in Codecov UI
networkFilter?: string // Specify a prefix on the files listed in the network section of the Codecov report. Useful for upload-specific path fixing
networkPrefix?: string // Specify a prefix on files listed in the network section of the Codecov report. Useful to help resolve path fixing
nonZero?: string // Should errors exit with a non-zero (default: false)
parent?: string // The commit SHA of the parent for which you are uploading coverage.
pr?: string // Specify the pull request number manually
preventSymbolicLinks?: string // Specifies whether to prevent following of symoblic links
rootDir?: string // Specify the project root directory when not in a git repo
sha?: string // Specify the commit SHA manually
slug: string // Specify the slug manually
source?: string // Track wrappers of the uploader
swift?: string // Run with swift support
swiftProject?: string // Specify the swift project
tag?: string // Specify the git tag
token?: string // Codecov upload token
upstream: string // Upstream proxy to connect to
url?: string // Change the upload host (Enterprise use)
useCwd?: boolean
verbose?: string // Run with verbose logging
xcode?: string // Run with xcode support
xcodeArchivePath?: string // Specify the xcode archive path. Likely specified as the -resultBundlePath and should end in .xcresult
}

export type UploaderEnvs = NodeJS.Dict<string>

export interface UploaderInputs {
envs: UploaderEnvs
args: UploaderArgs
}

export interface IProvider {
detect: (arg0: UploaderEnvs) => boolean
getServiceName: () => string
getServiceParams: (arg0: UploaderInputs) => IServiceParams
getEnvVarNames: () => string[]
}

export interface IServiceParams {
branch: string
// build: string
// buildURL: string
commit: string
// job: string
// pr: string | ''
service: string
slug: string
// name?: string
// tag?: string
// flags?: string
// parent?: string
// project?: string
// server_uri?: string
}

// export interface IRequestHeaders {
// agent?: ProxyAgent
// url: URL
// options: Dispatcher.RequestOptions
// }

export interface PostResults {
putURL: URL
resultURL: URL
}

export interface PutResults {
status: string
resultURL: URL
}

export type XcodeCoverageFileReport = Record<string, string | null>
export type XcodeCoverageReport = Record<string, XcodeCoverageFileReport>
Loading

0 comments on commit 285f6f5

Please sign in to comment.