Skip to content

Commit

Permalink
merge classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ovcharenko-di committed Dec 16, 2024
1 parent 102ad0a commit bcc368c
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 118 deletions.
114 changes: 109 additions & 5 deletions src/ru/pulsar/jenkins/library/steps/WithCoverage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.configuration.StepCoverageOptions
import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.utils.CoverageUtils
import ru.pulsar.jenkins.library.utils.FileUtils
import ru.pulsar.jenkins.library.utils.Logger

class WithCoverage implements Serializable {
Expand All @@ -28,22 +28,23 @@ class WithCoverage implements Serializable {
return
}

def context = CoverageUtils.prepareContext(config, coverageOptions)
def context = prepareContext(config, coverageOptions)
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()

steps.lock(context.lockableResource) {
try {

CoverageUtils.startCoverage(steps, config, context, stage)
startCoverage(steps, config, context, stage)

body()

CoverageUtils.stopCoverage(steps, config, context)
stopCoverage(steps, config, context)

steps.stash(stage.getCoverageStashName(), stage.getCoverageStashPath(), true)

} catch (Exception e) {
throw new Exception("При выполнении блока произошла ошибка: ${e}")
Logger.println("При выполнении блока произошла ошибка: ${e.message}")
throw e
} finally {

String pidsFilePath = "build/${stage.getStageSlug()}-pids"
Expand Down Expand Up @@ -78,4 +79,107 @@ class WithCoverage implements Serializable {
}
}
}

static List<String> getPIDs(String name) {

IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()

String pids
def script

if (steps.isUnix()) {
script = "ps -C '$name' -o pid= || true"
pids = steps.sh(script, false, true, 'UTF-8')
} else {
script = """@echo off
chcp 65001 > nul
tasklist | findstr "${name}" > nul
if errorlevel 1 (
exit /b 0
) else (
for /f "tokens=2" %%a in ('tasklist ^| findstr "${name}"') do (@echo %%a)
)"""
pids = steps.bat(script, false, true, 'UTF-8')
}
return pids.split('\r?\n').toList()
}

static CoverageContext prepareContext(JobConfiguration config, StepCoverageOptions options) {

IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
def env = steps.env()

def coverageOpts = config.coverageOptions
def port = options.dbgsPort
def currentDbgsPids = getPIDs("dbgs")
def currentCoverage41CPids = getPIDs("Coverage41C")
def lockableResource = "${env.NODE_NAME}_$port"

return new CoverageContext(lockableResource, config.srcDir, coverageOpts, port, currentDbgsPids, currentCoverage41CPids)

}

static void startCoverage(IStepExecutor steps, JobConfiguration config, CoverageContext coverageContext, Coverable stage) {

def env = steps.env()
def srcDir = config.srcDir
def workspaceDir = FileUtils.getFilePath("$env.WORKSPACE")

def coverageOpts = config.coverageOptions

String dbgsPath = findDbgs(steps, config)

steps.start(dbgsPath, "--addr=127.0.0.1 --port=${coverageContext.port}")
steps.start(coverageOpts.coverage41CPath, "start -i DefAlias -u http://127.0.0.1:${coverageContext.port} -P $workspaceDir -s $srcDir -o ${stage.getCoverageStashPath()}")
steps.cmd("${coverageOpts.coverage41CPath} check -i DefAlias -u http://127.0.0.1:${coverageContext.port}")

def newDbgsPids = getPIDs("dbgs")
def newCoverage41CPids = getPIDs("Coverage41C")

newDbgsPids.removeAll(coverageContext.dbgsPids)
newCoverage41CPids.removeAll(coverageContext.coverage41CPids)

newDbgsPids.addAll(newCoverage41CPids)
def pids = newDbgsPids.join(" ")

steps.writeFile(stage.getCoveragePidsPath(), pids, 'UTF-8')

Logger.println("PID процессов dbgs и Coverage41C для ${stage.getStageSlug()}: $pids")
}

static void stopCoverage(IStepExecutor steps, JobConfiguration config, CoverageContext coverageContext) {

def coverageOpts = config.coverageOptions

steps.cmd("${coverageOpts.coverage41CPath} stop -i DefAlias -u http://127.0.0.1:$coverageContext.port")
}

static String findDbgs(IStepExecutor steps, JobConfiguration config) {
if (steps == null || config == null) {
throw new IllegalArgumentException("Некорректные параметры поиска dbgs")
}

String dbgsPath = config.coverageOptions.dbgsPath
if (!dbgsPath.isEmpty()) {
Logger.println("Использую путь к dbgs из параметра dbgsPath: $dbgsPath")
return dbgsPath.strip()
}

final dbgsFindScriptPath = "build/tmp/dbgs_${System.currentTimeMillis()}.os"
final dbgsPathResult = "build/tmp/dbgsPath_${System.currentTimeMillis()}"

def dbgsFindScript = steps.libraryResource("dbgs.os")
steps.writeFile(dbgsFindScriptPath, dbgsFindScript, 'UTF-8')

steps.cmd("oscript ${dbgsFindScriptPath} ${config.v8version} > ${dbgsPathResult}")
dbgsPath = steps.readFile(dbgsPathResult).strip()

if (dbgsPath.isEmpty()) {
steps.error("Не удалось найти путь к dbgs")
}

Logger.println("Найден путь к dbgs: ${dbgsPath}")
return dbgsPath
}

}
113 changes: 0 additions & 113 deletions src/ru/pulsar/jenkins/library/utils/CoverageUtils.groovy

This file was deleted.

0 comments on commit bcc368c

Please sign in to comment.