@@ -2,7 +2,7 @@ import { deepmerge, dotenv, parse, path, ulid, ValidationError, z } from '../dep
22import { CliOptions , RunrealConfig } from '../lib/types.ts'
33import { execSync } from '../lib/utils.ts'
44import { ConfigSchema , InternalSchema } from '../lib/schema.ts'
5- import { Source } from './source.ts'
5+ import { Git , Perforce , Source } from './source.ts'
66import { renderConfig } from './template.ts'
77
88const env = ( key : string ) => Deno . env . get ( key ) || ''
@@ -16,15 +16,11 @@ export class Config {
1616 project : {
1717 name : '' ,
1818 path : '' ,
19+ buildPath : '' ,
1920 repoType : 'git' ,
2021 } ,
2122 build : {
22- path : '' ,
2323 id : env ( 'RUNREAL_BUILD_ID' ) || '' ,
24- branch : '' ,
25- branchSafe : '' ,
26- commit : '' ,
27- commitShort : '' ,
2824 } ,
2925 buildkite : {
3026 branch : env ( 'BUILDKITE_BRANCH' ) || '' ,
@@ -34,7 +30,17 @@ export class Config {
3430 buildPipelineSlug : env ( 'BUILDKITE_PIPELINE_SLUG' ) || '' ,
3531 } ,
3632 metadata : {
37- test : env ( 'RUNREAL_BUILD_ID' ) || '' ,
33+ safeRef : '' ,
34+ git : {
35+ branch : '' ,
36+ branchSafe : '' ,
37+ commit : '' ,
38+ commitShort : '' ,
39+ } ,
40+ perforce : {
41+ changelist : '' ,
42+ stream : '' ,
43+ } ,
3844 } ,
3945 workflows : [ ] ,
4046 }
@@ -43,7 +49,7 @@ export class Config {
4349 'branch' : 'engine.branch' ,
4450 'cachePath' : 'engine.cachePath' ,
4551 'projectPath' : 'project.path' ,
46- 'buildPath' : 'build.path ' ,
52+ 'buildPath' : 'project.buildPath ' ,
4753 'buildId' : 'build.id' ,
4854 'gitDependenciesCachePath' : 'git.dependenciesCachePath' ,
4955 'gitMirrors' : 'git.mirrors' ,
@@ -82,17 +88,6 @@ export class Config {
8288 return this . config
8389 }
8490
85- determineBuildId ( ) {
86- const build = this . config . build
87- if ( ! build ) return ulid ( )
88- if ( ! this . config . project ?. path ) return ulid ( )
89- if ( ! this . config . project ?. repoType ) return ulid ( )
90- const source = Source ( this . config . project ?. path , this . config . project ?. repoType )
91- const safeRef = source . safeRef ( )
92- if ( ! safeRef ) return ulid ( )
93- return safeRef
94- }
95-
9691 private async searchForConfigFile ( ) : Promise < string | null > {
9792 const cwd = Deno . cwd ( )
9893 const configPath = path . join ( cwd , 'runreal.config.json' )
@@ -143,8 +138,8 @@ export class Config {
143138 config . project . path = path . resolve ( config . project . path )
144139 }
145140
146- if ( config . build && config . build . path ) {
147- config . build . path = path . resolve ( config . build . path )
141+ if ( config . project && config . project . buildPath ) {
142+ config . project . buildPath = path . resolve ( config . project . buildPath )
148143 }
149144
150145 if ( config . git && config . git . dependenciesCachePath ) {
@@ -158,31 +153,91 @@ export class Config {
158153 return config
159154 }
160155
161- private populateBuild ( ) : RunrealConfig [ 'build ' ] | null {
156+ private getBuildMetadata ( ) : RunrealConfig [ 'metadata ' ] | null {
162157 const cwd = this . config . project ?. path
163158 if ( ! cwd ) return null
159+ if ( this . config . project ?. repoType === 'git' ) {
160+ const { safeRef, git } = this . getGitBuildMetadata ( cwd )
161+ return {
162+ safeRef,
163+ git,
164+ }
165+ } else if ( this . config . project ?. repoType === 'perforce' ) {
166+ const { safeRef, perforce } = this . getPerforceBuildMetadata ( cwd )
167+ return {
168+ safeRef,
169+ perforce,
170+ }
171+ }
172+ return null
173+ }
174+
175+ private getGitBuildMetadata ( projectPath : string ) : RunrealConfig [ 'metadata' ] {
176+ const cwd = projectPath
164177 try {
165- let branch : string
166- // On Buildkite, use the BUILDKITE_BRANCH env var as we may be in a detached HEAD state
167- if ( Deno . env . get ( 'BUILDKITE_BRANCH' ) ) {
168- branch = this . config . buildkite ?. branch || ''
169- } else {
170- branch = execSync ( 'git' , [ 'branch' , '--show-current' ] , { cwd, quiet : false } ) . output . trim ( )
178+ const source = new Git ( cwd )
179+ const branch = source . branch ( )
180+ const branchSafe = source . branchSafe ( )
181+ const commit = source . commit ( )
182+ const commitShort = source . commitShort ( )
183+ const safeRef = source . safeRef ( )
184+ return {
185+ safeRef,
186+ git : {
187+ branch,
188+ branchSafe,
189+ commit,
190+ commitShort,
191+ } ,
192+ }
193+ } catch ( e ) {
194+ return {
195+ safeRef : '' ,
196+ git : {
197+ branch : '' ,
198+ branchSafe : '' ,
199+ commit : '' ,
200+ commitShort : '' ,
201+ } ,
171202 }
172- const branchSafe = branch . replace ( / [ ^ a - z 0 - 9 ] / gi, '-' )
173- const commit = execSync ( 'git' , [ 'rev-parse' , 'HEAD' ] , { cwd, quiet : false } ) . output . trim ( )
174- const commitShort = execSync ( 'git' , [ 'rev-parse' , '--short' , 'HEAD' ] , { quiet : false } ) . output . trim ( )
203+ }
204+ }
175205
206+ private getPerforceBuildMetadata ( projectPath : string ) : RunrealConfig [ 'metadata' ] {
207+ const cwd = projectPath
208+ try {
209+ const source = new Perforce ( cwd )
210+ const changelist = source . changelist ( )
211+ const stream = source . stream ( )
212+ const safeRef = source . safeRef ( )
176213 return {
177- ...this . config . build ,
178- path : this . config . build ?. path || '' ,
179- branch,
180- branchSafe,
181- commit,
182- commitShort,
214+ safeRef,
215+ perforce : {
216+ changelist,
217+ stream,
218+ } ,
183219 }
184220 } catch ( e ) {
185- return null
221+ return {
222+ safeRef : '' ,
223+ perforce : {
224+ changelist : '' ,
225+ stream : '' ,
226+ } ,
227+ }
228+ }
229+ }
230+
231+ getBuildId ( ) {
232+ if ( this . config . build ?. id ) return this . config . build . id
233+ if ( ! this . config . project ?. path ) return ulid ( )
234+ if ( ! this . config . project ?. repoType ) return ulid ( )
235+ try {
236+ const source = Source ( this . config . project . path , this . config . project . repoType )
237+ const safeRef = source . safeRef ( )
238+ return safeRef
239+ } catch ( e ) {
240+ return ulid ( )
186241 }
187242 }
188243
@@ -192,10 +247,16 @@ export class Config {
192247 const Merged = ConfigSchema . and ( InternalSchema )
193248 this . config = Merged . parse ( this . config )
194249
195- const bd = this . populateBuild ( )
196- if ( bd ) this . config . build = bd
197- if ( ! this . config . build ?. id ) {
198- this . config . build ! . id = this . determineBuildId ( )
250+ const metadata = this . getBuildMetadata ( )
251+ this . config . metadata = {
252+ ...this . config . metadata ,
253+ ...metadata ,
254+ }
255+
256+ const buildId = this . getBuildId ( )
257+ this . config . build = {
258+ ...this . config . build ,
259+ id : buildId ,
199260 }
200261 } catch ( e ) {
201262 if ( e instanceof z . ZodError ) {
0 commit comments