Skip to content

Commit

Permalink
feat: infer Beat Saber version from manifest.json if not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
ChecksumDev committed Jul 18, 2024
1 parent cf43de7 commit de0df7d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ To quickly set up the modding environment, add the following step to your GitHub
uses: beat-forge/init-beatsaber@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: 1.37.1
```
This will clone the stripped Beat Saber references for version `1.29.1` to the default path `./Refs`.
This will clone the stripped Beat Saber references to the default path `./Refs` and infer the Beat Saber version from your `manifest.json` file.

## Inputs ⚙️

| Name | Description | Required | Default |
| --------- | ------------------------------------------------------------ | -------- | ----------------------------------------------------------------------------------- |
| `token` | GitHub token for cloning the Beat Saber repository. | Yes | None |
| `version` | Version of Beat Saber for the modding environment. | Yes | None |
| `path` | Path to clone the stripped Beat Saber references to. | No | `./Refs` |
| `host` | Host to clone the stripped Beat Saber references from. | No | `github.com` |
| `repo` | Repository to clone the stripped Beat Saber references from. | No | [`beat-forge/beatsaber-stripped`](https://github.com/beat-forge/beatsaber-stripped) |
| Name | Description | Required | Default |
| ---------- | ---------------------------------------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------- |
| `token` | GitHub token for cloning the Beat Saber repository. | Yes | None |
| `version` | Version of Beat Saber for the modding environment. If not specified, the action will try to infer it from a `manifest.json`. | No | None |
| `manifest` | Path to a specific `manifest.json` file for version inference. If not provided, the action will search recursively. | No | None |
| `path` | Path to clone the stripped Beat Saber references to. | No | `./Refs` |
| `host` | Host to clone the stripped Beat Saber references from. | No | `github.com` |
| `repo` | Repository to clone the stripped Beat Saber references from. | No | [`beat-forge/beatsaber-stripped`](https://github.com/beat-forge/beatsaber-stripped) |

## Contributing 🤝

Expand All @@ -38,4 +38,4 @@ Contributions, issues, and feature requests are welcome! Feel free to check out
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=beat-forge/init-beatsaber&type=Date" />
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=beat-forge/init-beatsaber&type=Date" />
</picture>
</a>
</a>
33 changes: 31 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,49 @@ async function moveContents(srcDir: string, destDir: string): Promise<void> {
core.info('Content move completed successfully')
}

async function findManifest(dir: string): Promise<string | null> {
const files = await fs.readdir(dir, { withFileTypes: true })
for (const file of files) {
const fullPath = join(dir, file.name)
if (file.isDirectory()) {
const manifest = await findManifest(fullPath)
if (manifest) return manifest
} else if (file.name === 'manifest.json') {
return fullPath
}
}
return null
}

async function run(): Promise<void> {
try {
core.info('Initializing Beat Saber references...')

const token = core.getInput('token', { required: true })
const requestedVersion = core.getInput('version', { required: true })
const manifestPath = core.getInput('manifest')
let requestedVersion = core.getInput('version')
const referencesPath = core.getInput('path') || './Refs'
const repo = core.getInput('repo') || 'beat-forge/beatsaber-stripped'
const host = core.getInput('host') || 'github.com'

core.info(
`Inputs: token=${token}, version=${requestedVersion}, path=${referencesPath}, repo=${repo}, host=${host}`
`Inputs: version=${requestedVersion}, path=${referencesPath}, repo=${repo}, host=${host}, manifest=${manifestPath}`
)

if (!requestedVersion) {
core.info('No version specified. Attempting to infer version from manifest.json...')
const manifestFile = manifestPath ? resolve(manifestPath) : await findManifest(process.cwd())
if (manifestFile) {
core.info(`Found manifest.json at ${manifestFile}`)
const manifest = JSON.parse(await fs.readFile(manifestFile, 'utf8'))
requestedVersion = manifest.gameVersion
core.info(`Inferred version: ${requestedVersion}`)
} else {
core.error('No manifest.json found and no version specified.')
throw new Error('No manifest.json found and no version specified.')
}
}

const [owner, repoName] = repo.split('/')
if (!owner || !repoName) {
core.error('Repository input is invalid. Expected format {owner}/{repo}.')
Expand Down

0 comments on commit de0df7d

Please sign in to comment.