/
WorkflowSessionImpl.kt
126 lines (97 loc) · 4.62 KB
/
WorkflowSessionImpl.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* See LICENSE in the project root for license information.
*/
package jetbrains.buildServer.agent.runner
import jetbrains.buildServer.RunBuildException
import jetbrains.buildServer.agent.*
import java.io.File
class WorkflowSessionImpl(
private val _workflowComposer: WorkflowComposer,
private val _buildStepContext: BuildStepContext,
private val _loggerService: LoggerService,
private val _argumentsService: ArgumentsService)
: MultiCommandBuildSession, WorkflowContext {
private var _commandLinesIterator: Iterator<CommandLine>? = null
private var _lastResult: CommandLineResult? = null
private var _buildFinishedStatus: BuildFinishedStatus? = null
override fun getNextCommand(): CommandExecution? {
val commandLinesIterator: Iterator<CommandLine> = _commandLinesIterator ?: _workflowComposer.compose(this).commandLines.iterator()
_commandLinesIterator = commandLinesIterator
if (status != WorkflowStatus.Running) {
return null
}
// yield command here
if (!commandLinesIterator.hasNext()) {
if (_buildFinishedStatus == null) {
_buildFinishedStatus = BuildFinishedStatus.FINISHED_SUCCESS
}
return null
}
val exitCode = ArrayList<Int>()
_lastResult = CommandLineResult(exitCode.asSequence(), emptySequence(), emptySequence())
return CommandExecutionAdapter(
commandLinesIterator.next(),
exitCode,
_buildStepContext,
_loggerService,
_argumentsService)
}
override val status: WorkflowStatus
get() {
val curStatus = _buildFinishedStatus
if (curStatus == null) {
return WorkflowStatus.Running
}
when (curStatus) {
BuildFinishedStatus.FINISHED_SUCCESS, BuildFinishedStatus.FINISHED_WITH_PROBLEMS -> return WorkflowStatus.Completed
else -> return WorkflowStatus.Failed
}
}
override fun abort(buildFinishedStatus: BuildFinishedStatus) {
_buildFinishedStatus = buildFinishedStatus
}
override fun sessionStarted() = Unit
override fun sessionFinished(): BuildFinishedStatus? =
_buildFinishedStatus ?: BuildFinishedStatus.FINISHED_SUCCESS
override val lastResult: CommandLineResult
get() = _lastResult ?: throw RunBuildException("There are no any results yet")
private class CommandExecutionAdapter(
private val _commandLine: CommandLine,
private val _exitCode: MutableCollection<Int>,
private val _buildStepContext: BuildStepContext,
private val _loggerService: LoggerService,
private val _argumentsService: ArgumentsService) : CommandExecution {
override fun beforeProcessStarted() = Unit
override fun processStarted(programCommandLine: String, workingDirectory: File) = Unit
override fun processFinished(exitCode: Int) {
_exitCode.add(exitCode)
}
override fun makeProgramCommandLine(): ProgramCommandLine = ProgramCommandLineAdapter(
_argumentsService,
_commandLine,
_buildStepContext.runnerContext.buildParameters.environmentVariables)
override fun onStandardOutput(text: String)= _loggerService.onStandardOutput(text)
override fun onErrorOutput(text: String) = _loggerService.onErrorOutput(text)
override fun interruptRequested(): TerminationAction = TerminationAction.KILL_PROCESS_TREE
override fun isCommandLineLoggingEnabled(): Boolean = true
}
private class ProgramCommandLineAdapter(
private val _argumentsService: ArgumentsService,
private val _commandLine: CommandLine,
private val _environmentVariables: Map<String, String>) : ProgramCommandLine {
override fun getExecutablePath(): String =
_commandLine.executableFile.absolutePath
override fun getWorkingDirectory(): String =
_commandLine.workingDirectory.absolutePath
override fun getArguments(): MutableList<String> =
_commandLine.arguments.map { it.value }.toMutableList()
override fun getEnvironment(): MutableMap<String, String> {
val environmentVariables = _environmentVariables.toMutableMap()
_commandLine.environmentVariables.forEach { environmentVariables[it.name] = it.value }
return environmentVariables
}
}
}