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

Include repo object when templating #303

Open
wants to merge 6 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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,37 @@ Result:
Created by Maxi (@BetaHuhn)
```

By default, every Nunjucks template context will be pre-populated with the `repo` object containing the following values:

- `repo.url`
- `repo.fullName`
- `repo.uniqueName`
- `repo.host`
- `repo.user`
- `repo.name`
- `repo.branch`

This can be useful for certain bulk templating use cases:

```yml
# sync.yml

group:
- repos: |
user/repo1
user/repo2
...
files:
- source: src/README.md
template: true
```

```yml
# README.md

Hello from {{ repo.name }} by {{ repo.user }}!
```

You can also use `extends` with a relative path to inherit other templates. Take a look at Nunjucks [template syntax](https://mozilla.github.io/nunjucks/templating.html) for more info.

```yml
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const parseRepoName = (fullRepo) => {
const branch = fullRepo.split('@')[1] || 'default'

return {
url: `https://${ host }/${ user }/${ name }`,
fullName: `${ host }/${ user }/${ name }`,
uniqueName: `${ host }/${ user }/${ name }@${ branch }`,
host,
Expand Down Expand Up @@ -248,4 +249,4 @@ export async function parseConfig() {
return Object.values(result)
}

export default context
export default context
14 changes: 9 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ export async function pathIsDirectory(path) {
return stat.isDirectory()
}

export async function write(src, dest, context) {
export async function write(src, dest, context, item) {
if (typeof context !== 'object') {
context = {}
}

// include current repo constants (e.g., host, user, name, branch, etc.)
context.repo = item.repo

const content = nunjucks.render(src, context)
await fs.outputFile(dest, content)
}

export async function copy(src, dest, isDirectory, file) {
export async function copy(src, dest, isDirectory, file, item) {
const deleteOrphaned = isDirectory && file.deleteOrphaned
const exclude = file.exclude

Expand Down Expand Up @@ -121,12 +125,12 @@ export async function copy(src, dest, isDirectory, file) {

const srcPath = path.join(src, srcFile)
const destPath = path.join(dest, srcFile)
await write(srcPath, destPath, file.template)
await write(srcPath, destPath, file.template, item)
}
} else {
core.debug(`Render file ${ src } to ${ dest }`)

await write(src, dest, file.template)
await write(src, dest, file.template, item)
}
} else {
core.debug(`Copy ${ src } to ${ dest }`)
Expand Down Expand Up @@ -166,4 +170,4 @@ export async function remove(src) {

export function arrayEquals(array1, array2) {
return Array.isArray(array1) && Array.isArray(array2) && array1.length === array2.length && array1.every((value, i) => value === array2[i])
}
}
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ async function run() {

await forEach(repos, async (item) => {
core.info(`Repository Info`)
core.info(`Slug : ${ item.repo.name }`)
core.info(`Owner : ${ item.repo.user }`)
core.info(`Https Url : https://${ item.repo.fullName }`)
core.info(`Branch : ${ item.repo.branch }`)
core.info(`Slug : ${ item.repo.name }`)
core.info(`Owner : ${ item.repo.user }`)
core.info(`Url : ${ item.repo.url }`)
core.info(`Branch : ${ item.repo.branch }`)
core.info(' ')
try {

Expand Down Expand Up @@ -74,7 +74,7 @@ async function run() {

if (isDirectory) core.info(`Source is directory`)

await copy(source, dest, isDirectory, file)
await copy(source, dest, isDirectory, file, item)

await git.add(file.dest)

Expand Down Expand Up @@ -220,4 +220,4 @@ run()
.catch((err) => {
core.setFailed(err.message)
core.debug(err)
})
})