-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ruby stack first implementation
- Loading branch information
1 parent
f257403
commit 3c21978
Showing
10 changed files
with
350 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
cli/tests/fixtures/ruby/rubocop-lint/expected/.github/workflows/pipelinit.ruby.lint.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated with pipelinit 0.2.0 | ||
# https://pipelinit.com/ | ||
name: Lint Ruby | ||
on: | ||
pull_request: | ||
paths: | ||
- "**.rb" | ||
jobs: | ||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: "2.5.0" | ||
bundler-cache: true | ||
- run: bundle exec rubocop --lint . |
Submodule project
added at
665f3b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Context } from "../../../types.ts"; | ||
|
||
const rubyDepRegex = /(?:(["'])(?<DependencyName>[a-zA-Z\-_\.]+)["'])/gm; | ||
|
||
function notEmpty<TValue>(value: TValue | null | undefined): value is TValue { | ||
return value !== null && value !== undefined; | ||
} | ||
|
||
const readDependencyFile = async (context: Context) => { | ||
for await (const file of await context.files.each("**/Gemfile")) { | ||
const rubyDepsText = await context.files.readText(file.path); | ||
|
||
const depsRuby: string[] = Array.from( | ||
rubyDepsText.matchAll(rubyDepRegex), | ||
(match) => !match.groups ? null : match.groups.DependencyName, | ||
).filter(notEmpty); | ||
|
||
if (depsRuby) { | ||
return depsRuby; | ||
} | ||
} | ||
return []; | ||
}; | ||
|
||
export const hasRubyDependency = async ( | ||
context: Context, | ||
dependencyName: string, | ||
): Promise<boolean> => { | ||
const dependencies = await readDependencyFile(context); | ||
return dependencies.some((dep) => dep === dependencyName); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { IntrospectFn } from "../../../types.ts"; | ||
import { introspect as introspectRubocop, Rubocop } from "./rubocop.ts"; | ||
|
||
export type Linters = { | ||
rubocop?: Rubocop; | ||
}; | ||
|
||
export const introspect: IntrospectFn<Linters> = async (context) => { | ||
const linters: Linters = {}; | ||
const logger = context.getLogger("ruby"); | ||
|
||
const rubocopInfo = await introspectRubocop(context); | ||
if (rubocopInfo) { | ||
logger.debug("detected Rubocop"); | ||
linters.rubocop = rubocopInfo; | ||
} else { | ||
if (context.suggestDefault) { | ||
logger.warning( | ||
"No linters for ruby were identified in the project, creating default pipeline with 'Rubocop' WITHOUT any specific configuration", | ||
); | ||
linters.rubocop = { | ||
isDependency: false, | ||
name: "rubocop", | ||
}; | ||
} | ||
} | ||
|
||
return linters; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Introspector } from "../../../types.ts"; | ||
import { introspect as introspectVersion } from "./version.ts"; | ||
import { introspect as introspectLinters, Linters } from "./linters.ts"; | ||
|
||
/** | ||
* Introspected information about a project with Ruby | ||
*/ | ||
export default interface RubyProject { | ||
/** | ||
* Ruby version | ||
*/ | ||
version?: string; | ||
/** | ||
* Which linter the project uses, if any | ||
*/ | ||
linters: Linters; | ||
} | ||
|
||
export const introspector: Introspector<RubyProject | undefined> = { | ||
detect: async (context) => { | ||
return await context.files.includes("**/*.rb"); | ||
}, | ||
introspect: async (context) => { | ||
const logger = context.getLogger("ruby"); | ||
|
||
// Version | ||
logger.debug("detecting version"); | ||
const version = await introspectVersion(context); | ||
if (version === undefined) { | ||
logger.debug("didn't detect the version"); | ||
return undefined; | ||
} | ||
logger.debug(`detected version ${version}`); | ||
|
||
// Linters and Formatters | ||
const linters = await introspectLinters(context); | ||
|
||
return { | ||
version: version, | ||
linters: linters, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { IntrospectFn } from "../../../types.ts"; | ||
import { hasRubyDependency } from "./dependencies.ts"; | ||
|
||
export interface Rubocop { | ||
name: "rubocop"; | ||
isDependency: boolean; | ||
} | ||
|
||
export const introspect: IntrospectFn<Rubocop | null> = async (context) => { | ||
const isDependency = await hasRubyDependency(context, "rubocop"); | ||
// Search for any of the following files: | ||
// .rubocop.yml | ||
// Reference: https://docs.rubocop.org/rubocop/configuration.html#config-file-locations | ||
const hasRubocopConfig = await context.files.includes( | ||
"**/.rubocop.yml", | ||
); | ||
|
||
if (hasRubocopConfig) { | ||
return { | ||
name: "rubocop", | ||
isDependency: isDependency, | ||
}; | ||
} else if (isDependency) { | ||
return { | ||
name: "rubocop", | ||
isDependency: true, | ||
}; | ||
} | ||
|
||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { IntrospectFn } from "../../../types.ts"; | ||
|
||
// Find the latest stable version here: | ||
// https://www.ruby-lang.org/en/downloads/ | ||
const LATEST = "3.0.2"; | ||
|
||
const WARN_USING_LATEST = | ||
`Couldn't detect the Ruby version, using the latest available: ${LATEST}`; | ||
|
||
const ERR_UNDETECTABLE_TITLE = | ||
"Couldn't detect which Ruby version this project uses."; | ||
const ERR_UNDETECTABLE_INSTRUCTIONS = ` | ||
To fix this issue, consider one of the following suggestions: | ||
1. Add the 'required_ruby_version' to your Gemfile | ||
The Gemfile permits the specification of the adequate ruby version for this package. | ||
See https://guides.rubygems.org/specification-reference/#required_ruby_version= | ||
2. Create a .ruby-version file | ||
The .ruby-version file can be used by the RVM(Ruby Version Manager) to choose a specific Ruby version | ||
for a project. | ||
Its the easiest option, all you have to do is create a .ruby-version text | ||
file with a version inside, like "3.0.1". | ||
See https://rvm.io/workflow/projects#project-file-ruby-version | ||
`; | ||
|
||
function notEmpty<TValue>(value: TValue | null | undefined): value is TValue { | ||
return value !== null && value !== undefined; | ||
} | ||
|
||
/** | ||
* Search for application specific `.ruby-version` file from RVM | ||
* | ||
* @see https://rvm.io/workflow/examples#rvm-info | ||
*/ | ||
const rvm: IntrospectFn<string> = async (context) => { | ||
for await (const file of context.files.each("**/.ruby-version")) { | ||
return await context.files.readText(file.path); | ||
} | ||
throw Error("Can't find ruby version at .ruby-version"); | ||
}; | ||
|
||
/** | ||
* Search a Gemfile for the required ruby version key | ||
*/ | ||
const gemfile: IntrospectFn<string> = async (context) => { | ||
for await (const file of context.files.each("./Gemfile")) { | ||
const gemfileText = await context.files.readText(file.path); | ||
|
||
const rubyVersion: string[] = Array.from( | ||
gemfileText.matchAll(/ruby ["'](?<RubyVersion>.*)["']/), | ||
// required_ruby_version.*(?<RubyVersion>[0-9]+\.[0-9]+\.[0-9]) | ||
(match) => !match.groups ? null : match.groups.RubyVersion, | ||
).filter(notEmpty); | ||
|
||
if (rubyVersion.length > 0) { | ||
return rubyVersion[0]; | ||
} | ||
} | ||
throw Error("Can't find ruby version at Gemfile"); | ||
}; | ||
|
||
/** | ||
* Search a .gemspec file looking for the key required_ruby_version | ||
* | ||
* @see https://guides.rubygems.org/specification-reference/#required_ruby_version= | ||
*/ | ||
const gemspec: IntrospectFn<string> = async (context) => { | ||
for await (const file of context.files.each("./*.gemspec")) { | ||
const gemspecText = await context.files.readText(file.path); | ||
|
||
const rubyVersion: string[] = Array.from( | ||
gemspecText.matchAll( | ||
/required_ruby_version.*(?<RubyVersion>[0-9]+\.[0-9]+\.[0-9])/gm, | ||
), | ||
(match) => !match.groups ? null : match.groups.RubyVersion, | ||
).filter(notEmpty); | ||
|
||
if (rubyVersion.length > 0) { | ||
return rubyVersion[0]; | ||
} | ||
} | ||
throw Error("Can't find ruby version at RubyVersion"); | ||
}; | ||
|
||
/** | ||
* Searches for the project Python version in multiple places, such as: | ||
* - .ruby-version | ||
* - Gemfile | ||
* - .gemspec | ||
* | ||
* If it fails to find a version definition anywhere, the next step depends | ||
* wheter Pipelinit is running in the strict mode. It emits an error if running | ||
* in the strict mode, otherwise it emits an warning and fallback to the latest | ||
* stable version. | ||
*/ | ||
export const introspect: IntrospectFn<string | undefined> = async (context) => { | ||
const logger = context.getLogger("ruby"); | ||
|
||
try { | ||
return await Promise.any([ | ||
rvm(context), | ||
gemfile(context), | ||
gemspec(context), | ||
]); | ||
} catch (error: unknown) { | ||
if (error instanceof AggregateError) { | ||
logger.debug(error.errors.map((e) => e.message).join("\n")); | ||
|
||
if (!context.strict) { | ||
logger.warning(WARN_USING_LATEST); | ||
return LATEST; | ||
} | ||
|
||
context.errors.add({ | ||
title: ERR_UNDETECTABLE_TITLE, | ||
message: ERR_UNDETECTABLE_INSTRUCTIONS, | ||
}); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<% if (it.linters) { -%> | ||
name: Lint Ruby | ||
on: | ||
pull_request: | ||
paths: | ||
- "**.rb" | ||
jobs: | ||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: "<%= it.version %>" | ||
bundler-cache: true | ||
<% if (!it.linters.rubocop.isDependency) { -%> | ||
- run: gem install | ||
<% } -%> | ||
<% if (!it.linters.rubocop.isDependency) { -%> | ||
- run: gem install rubocop | ||
<% } -%> | ||
<% if (it.linters.rubocop) { -%> | ||
- run: bundle exec rubocop --lint . | ||
<% } -%> | ||
<%- } -%> |