Skip to content

Commit

Permalink
codegen to generate samples and move to azure-resourcemanager
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft committed Nov 16, 2021
1 parent dc1e2a0 commit 880527f
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions sdk/resourcemanager/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ function handleInput(projects, cb) {
}

function codegen(project, cb) {
packagePath = mappings[project].package.replace(/\./g, '/');

if (!args['preserve']) {
const sourcesToDelete = path.join(
mappings[project].dir,
'/src/main/java/',
mappings[project].package.replace(/\./g, '/'));
packagePath);

deleteFolderRecursive(sourcesToDelete);
}
Expand All @@ -127,6 +129,7 @@ function codegen(project, cb) {
' --java ' +
' --azure-arm ' +
' --pipeline.modelerfour.additional-checks=false --pipeline.modelerfour.lenient-model-deduplication=true ' +
' --generate-samples ' +
generator +
` --java.namespace=${mappings[project].package} ` +
` --java.output-folder=${outDir} ` +
Expand All @@ -144,7 +147,15 @@ function codegen(project, cb) {
}

console.log('Command: ' + cmd);
return execa(cmd, [], { shell: true, stdio: "inherit" });
autorest_result = execa.sync(cmd, [], { shell: true, stdio: "inherit" });

// move generated samples to azure-resourcemanager
generatedSamplesSource = path.join(mappings[project].dir, '/src/samples/java/', packagePath, 'generated');
generatedSamplesTarget = path.join('azure-resourcemanager/src/samples/java/', packagePath);
copyFolderRecursiveSync(generatedSamplesSource, generatedSamplesTarget);
deleteFolderRecursive(generatedSamplesSource);

return autorest_result;
};

function deleteFolderRecursive(path) {
Expand All @@ -164,6 +175,43 @@ function deleteFolderRecursive(path) {
}
};

function copyFileSync(source, target) {

var targetFile = target;

// If target is a directory, a new file with the same name will be created
if ( fs.existsSync( target ) ) {
if ( fs.lstatSync( target ).isDirectory() ) {
targetFile = path.join( target, path.basename( source ) );
}
}

fs.writeFileSync(targetFile, fs.readFileSync(source));
}

function copyFolderRecursiveSync(source, target) {
var files = [];

// Check if folder needs to be created or integrated
var targetFolder = path.join( target, path.basename( source ) );
if ( !fs.existsSync( targetFolder ) ) {
fs.mkdirSync( targetFolder, { recursive: true } );
}

// Copy
if ( fs.lstatSync( source ).isDirectory() ) {
files = fs.readdirSync( source );
files.forEach( function ( file ) {
var curSource = path.join( source, file );
if ( fs.lstatSync( curSource ).isDirectory() ) {
copyFolderRecursiveSync( curSource, targetFolder );
} else {
copyFileSync( curSource, targetFolder );
}
} );
}
}

async function prepareBuild() {
return shell.task('mvn package javadoc:aggregate -DskipTests -q');
}
Expand Down

0 comments on commit 880527f

Please sign in to comment.