forked from microsoft/azure-pipelines-tasks
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JavaToolInstallerV0] add Maven Toolchains declaration (microsoft#16492)
Add Maven Toolchains declaration generation based on the JDK input parameters
- Loading branch information
Showing
9 changed files
with
318 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import mockanswer = require('azure-pipelines-task-lib/mock-answer'); | ||
import tmrm = require('azure-pipelines-task-lib/mock-run'); | ||
import msRestAzure = require('azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common'); | ||
import path = require('path'); | ||
import os = require('os'); | ||
import mockTask = require('azure-pipelines-task-lib/mock-task'); | ||
|
||
const taskPath = path.join(__dirname, '..', 'javatoolinstaller.js'); | ||
const tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); | ||
|
||
const destDir = path.join(os.homedir(), '.m2'); | ||
const destFile = path.join(destDir, 'toolchains.xml'); | ||
|
||
tr.setInput("versionSpec", "8.1"); | ||
tr.setInput("jdkSource", "Azure Storage") | ||
tr.setInput("artifactProvider", "azureStorage"); | ||
tr.setInput("azureResourceManagerEndpoint", "ARM1"); | ||
tr.setInput("azureStorageAccountName", "storage1"); | ||
tr.setInput("azureContainerName", "container1"); | ||
tr.setInput("azureCommonVirtualPath", ""); | ||
tr.setInput("fileType", ".tar.gz"); | ||
tr.setInput("destinationFolder", "javaJDK"); | ||
tr.setInput("cleanDestinationFolder", "false"); | ||
tr.setInput("createMavenToolchains", "true"); | ||
|
||
// provide answers for task mock | ||
const a: mockanswer.TaskLibAnswers = <mockanswer.TaskLibAnswers>{ | ||
exist: {[destDir]: true, [destFile]: true}, | ||
find: {[destDir]: [destDir], [destFile]: [destFile]}, | ||
rmRF: { }, | ||
stats: {[destDir]: {'isDirectory':'true'}, [destFile]: {'isFile':'true'}}, | ||
}; | ||
tr.setAnswers(a); | ||
|
||
tr.registerMock("azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-storage", { | ||
StorageManagementClient: function (A, B) { | ||
return { | ||
storageAccounts: { | ||
get: function (A) { | ||
return { | ||
properties: { | ||
primaryEndpoints: { | ||
blob: "primaryBlobUrl" | ||
} | ||
}, | ||
id: "StorageAccountUrl" | ||
} | ||
}, | ||
listkeys: function (A, B, C) { | ||
return ["accesskey1", "accessKey2"]; | ||
} | ||
} | ||
} | ||
}, | ||
StorageAccounts: { | ||
getResourceGroupNameFromUri: function (A) { | ||
return "storageAccountResouceGroupName"; | ||
} | ||
} | ||
}); | ||
|
||
tr.registerMock("azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common", { | ||
ApplicationTokenCredentials: function(A,B,C,D,E,F,G) { | ||
return {}; | ||
} | ||
}); | ||
|
||
tr.registerMock('./AzureStorageArtifacts/AzureStorageArtifactDownloader',{ | ||
AzureStorageArtifactDownloader: function(A,B,C) { | ||
return { | ||
downloadArtifacts: function(A,B) { | ||
return "pathFromDownloader"; | ||
} | ||
} | ||
} | ||
}) | ||
|
||
const mtl = require("azure-pipelines-tool-lib/tool") | ||
const mtlClone = Object.assign({}, mtl); | ||
|
||
mtlClone.prependPath = function(variable1: string, variable2: string) { | ||
return {}; | ||
}; | ||
|
||
tr.registerMock("azure-pipelines-tool-lib/tool", mtlClone); | ||
|
||
const mfs = require('fs') | ||
const mfsClone = Object.assign({}, mfs); | ||
|
||
mfsClone.lstatSync = function(variable: string) { | ||
return { | ||
isDirectory: function() { | ||
return true; | ||
} | ||
}; | ||
}; | ||
|
||
mfsClone.existsSync = function (variable: string) { | ||
if (variable === "DestinationDirectory\\econdlevelJDK2" || variable === "DestinationDirectory/econdlevelJDK2") { | ||
return false; | ||
} else return true; | ||
} | ||
|
||
tr.registerMock('fs', mfsClone); | ||
|
||
tr.run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import fs = require('fs'); | ||
import os = require('os'); | ||
import path = require('path'); | ||
import taskLib = require('azure-pipelines-task-lib/task'); | ||
import toolLib = require('azure-pipelines-tool-lib/tool'); | ||
import { create as xmlCreate } from 'xmlbuilder2'; | ||
|
||
const M2_DIR = '.m2'; | ||
const MVN_TOOLCHAINS_FILE: string = 'toolchains.xml'; | ||
|
||
export async function configureToolchains(version: string, vendor: string, jdkHome: string) { | ||
const id = `${vendor}_${version}`; | ||
const settingsDirectory = path.join(os.homedir(), M2_DIR); | ||
|
||
await createToolchainsSettings( | ||
version, | ||
vendor, | ||
id, | ||
jdkHome, | ||
settingsDirectory, | ||
); | ||
} | ||
|
||
async function createToolchainsSettings( | ||
version: string, | ||
vendor: string, | ||
id: string, | ||
jdkHome: string, | ||
settingsDirectory: string, | ||
) { | ||
toolLib.debug(`Creating ${MVN_TOOLCHAINS_FILE} for JDK version ${version} from ${vendor}`); | ||
await taskLib.mkdirP(settingsDirectory); | ||
const originalToolchains = await readExisting(settingsDirectory); | ||
const updatedToolchains = generate(originalToolchains, version, vendor, id, jdkHome); | ||
await write(settingsDirectory, updatedToolchains, true); | ||
} | ||
|
||
function generate( | ||
original: string, | ||
version: string, | ||
vendor: string, | ||
id: string, | ||
jdkHome: string | ||
) { | ||
let xmlObj; | ||
if (original && original.length > 0) { | ||
xmlObj = xmlCreate(original) | ||
.root() | ||
.ele({ | ||
type: 'jdk', | ||
provides: { | ||
version: `${version}`, | ||
vendor: `${vendor}`, | ||
id: `${id}` | ||
}, | ||
configuration: { | ||
jdkHome: `${jdkHome}` | ||
} | ||
}); | ||
} else | ||
xmlObj = xmlCreate({ | ||
toolchains: { | ||
'@xmlns': 'https://maven.apache.org/TOOLCHAINS/1.1.0', | ||
'@xmlns:xsi': 'https://www.w3.org/2001/XMLSchema-instance', | ||
'@xsi:schemaLocation': | ||
'https://maven.apache.org/TOOLCHAINS/1.1.0 https://maven.apache.org/xsd/toolchains-1.1.0.xsd', | ||
toolchain: [ | ||
{ | ||
type: 'jdk', | ||
provides: { | ||
version: `${version}`, | ||
vendor: `${vendor}`, | ||
id: `${id}` | ||
}, | ||
configuration: { | ||
jdkHome: `${jdkHome}` | ||
} | ||
} | ||
] | ||
} | ||
}); | ||
|
||
return xmlObj.end({ | ||
format: 'xml', | ||
wellFormed: false, | ||
headless: false, | ||
prettyPrint: true, | ||
width: 80 | ||
}); | ||
} | ||
|
||
async function readExisting(directory: string) { | ||
const location = path.join(directory, MVN_TOOLCHAINS_FILE); | ||
if (fs.existsSync(location)) { | ||
return fs.readFileSync(location, { | ||
encoding: 'utf-8', | ||
flag: 'r' | ||
}); | ||
} | ||
return ''; | ||
} | ||
|
||
async function write(directory: string, settings: string, overwriteSettings: boolean) { | ||
const location = path.join(directory, MVN_TOOLCHAINS_FILE); | ||
const settingsExists = fs.existsSync(location); | ||
if (settingsExists && overwriteSettings) { | ||
toolLib.debug(`Overwriting existing file ${location}`); | ||
} else if (!settingsExists) { | ||
toolLib.debug(`Writing to ${location}`); | ||
} else { | ||
toolLib.debug( | ||
`Skipping generation ${location} because file already exists and overwriting is not required` | ||
); | ||
return; | ||
} | ||
|
||
return fs.writeFileSync(location, settings, { | ||
encoding: 'utf-8', | ||
flag: 'w' | ||
}); | ||
} |