Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add projectspecific files based on directory #140

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 132 additions & 38 deletions generators/add/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ const path = require('path');
const chalk = require('chalk');
const util = require('../app/utility');

// remember not to copy files in _copySerializationItems() if a project specific one has been found
var usingCustomSerializationConfig = false;

module.exports = class extends yeoman {

constructor(args, opts) {
super(args, opts);
this.argument('ProjectName', { type: String, required: false, desc: 'Name of the project' });
this.argument('VendorPrefix', { type: String, required: false, desc: 'Vendor prefix used in the creation of project structure' });
}

init() {
this.log(yosay('Lets generate that project!'));
this.templatedata = {};
Expand All @@ -35,6 +39,12 @@ module.exports = class extends yeoman {
message: 'Would you like to include Unicorn (serialization)?',
default : true
},
{
type: 'confirm',
name: 'generateBuildConfigs',
message: 'Should build configurations be generated (should be deactivated if your .csproj templates already provide build configurations)?',
default : true
},
{
type:'input',
name:'sourceFolder',
Expand Down Expand Up @@ -99,7 +109,7 @@ module.exports = class extends yeoman {
message: 'Enter optional Module Group '
}];

this.prompt(questions).then((answers) => {
this.prompt(questions).then((answers) => {
this.modulegroup = answers.modulegroup ? answers.modulegroup : '';
done();
});
Expand Down Expand Up @@ -135,7 +145,7 @@ module.exports = class extends yeoman {

_copyProjectItems() {
mkdir.sync(this.settings.ProjectPath);
if(this.settings.serialization) {
if(this._serializationIsEnabled()) {
this.fs.copyTpl(
this.templatePath('_project.unicorn.csproj'),
this.destinationPath(
Expand Down Expand Up @@ -179,67 +189,151 @@ module.exports = class extends yeoman {
});
}

_copySolutionSpecificItems(){
this.fs.copyTpl(
this.destinationPath('helix-template/**/*'),
this.destinationPath(this.settings.ProjectPath),
this.templatedata);
// copy all templates from the helix-template folder and replace tokens in filepaths
_copySolutionSpecificItems(path = "", adjustedPath = ""){
var sourcePath = this.destinationPath('helix-template/' + path);
var files = fs.readdirSync(sourcePath, {withFileTypes: true});
files.forEach(file => {

var destinationPath = this.settings.ProjectPath + '/' + adjustedPath;

// call this function recursively for child directories
var stats = fs.statSync(sourcePath + file);
if(stats.isDirectory())
{

var childPath = path + file + '/';
var adjustedChildPath = path + this._replaceTokensInFileName(file);
if (!fs.existsSync(this.destinationPath(destinationPath)))
{
fs.mkdirSync(this.destinationPath(destinationPath));
}
this._copySolutionSpecificItems(childPath, adjustedChildPath);
}

// copy template
else {

// copy the project file which does contain serialization configurations (according to file ending)
if(file.toLowerCase().endsWith('.unicorn.csproj'))
{
// serialization is enabled and the current file is the .csproj with serialization enabled
if(this._serializationIsEnabled())
{
// copy template but remove the unicorn suffix
this.fs.copyTpl(
sourcePath + file,
destinationPath + '/' + this._replaceTokensInFileName(file.replace("unicorn.", "")),
this.templatedata);
}
return;
}

// if serialization is enabled, do not copy the project file which
// does not contain any serialization configurations (according to file ending)
if (file.toLowerCase().endsWith('.csproj') && this._serializationIsEnabled())
{
return;
}

// only copy serialization.config if serialization is enabled
if(file.toLowerCase().endsWith('serialization.config') && !this._serializationIsEnabled())
{
return;
}

// remember not to copy serialization file in _copySerializationItems() if a project specific one has been found
if(file.toLowerCase().endsWith('serialization.config'))
{
usingCustomSerializationConfig = true;
}

this.fs.copyTpl(
sourcePath + file,
destinationPath + '/' + this._replaceTokensInFileName(file),
this.templatedata);
}
});


}

_renameProjectFile() {
fs.renameSync(
this.destinationPath(
path.join(this.settings.ProjectPath, '_project.csproj')
),
this.destinationPath(
path.join(
this.settings.ProjectPath,
this.settings.LayerPrefixedProjectName + '.csproj'
)
)
);
// Indicates if searialization is enabled or not
_serializationIsEnabled() {
return this.settings.serialization;
}

// replace all occurences of "_Layer" "_Module" "_Vendor" in the provided string with the current values
_replaceTokensInFileName(filename = ""){
filename = filename.replace("_Layer", this.layer);
filename = filename.replace("_Module", this.settings.ProjectName);
filename = filename.replace("_Vendor", this.settings.VendorPrefix);
return filename;
}

_renameProjectFile() {
// select correct .csproj file to rename depending on whether or not serialization is enabled
var filename = (this._serializationIsEnabled()) ? '_project.unicorn.csproj' : '_project.csproj';

if(fs.existsSync(this.destinationPath('helix-template/' + filename))){
fs.renameSync(
this.destinationPath(
path.join(this.settings.ProjectPath, '_project.csproj')
),
this.destinationPath(
path.join(
this.settings.ProjectPath,
this.settings.LayerPrefixedProjectName + '.csproj'
)
)
);
}
}
_copySerializationItems() {
if(this.modulegroup){
mkdir.sync(path.join(this.settings.sourceFolder, this.layer, this.modulegroup, this.settings.ProjectName, 'serialization' ));
}
else{
mkdir.sync(path.join(this.settings.sourceFolder, this.layer, this.settings.ProjectName, 'serialization' ));
}
const serializationDestinationFile = path.join(
this.settings.ProjectPath,
'App_Config/Include',
this.settings.LayerPrefixedProjectName,
'serialization.config'
);

this.fs.copyTpl(this.templatePath('_serialization.config'), this.destinationPath(serializationDestinationFile), this.templatedata);

// only copy _serialization.config if there is no project specific one
if(!usingCustomSerializationConfig)
{

const serializationDestinationFile = path.join(
this.settings.ProjectPath,
'App_Config/Include',
this.settings.LayerPrefixedProjectName,
'serialization.config'
);

this.fs.copyTpl(this.templatePath('_serialization.config'), this.destinationPath(serializationDestinationFile), this.templatedata);
}
}

writing() {
this.settings.ProjectPath = path.join(this.settings.sourceFolder, this.layer, this.modulegroup, this.settings.ProjectName, 'code' );
this._copyProjectItems();

if(this.settings.serialization) {
this._copySerializationItems();
}
this._copyProjectItems();

if(fs.existsSync(this.destinationPath('helix-template'))) {
this._copySolutionSpecificItems();
}


if(this._serializationIsEnabled()) {
this._copySerializationItems();
}

const files = fs.readdirSync(this.destinationPath());
const SolutionFile = files.find(file => file.toUpperCase().endsWith(".SLN"));
const scriptParameters = '-SolutionFile \'' + this.destinationPath(SolutionFile) + '\' -Name ' + this.settings.LayerPrefixedProjectName + ' -Type ' + this.layer + ' -ProjectPath \'' + this.settings.ProjectPath + '\'' + ' -SolutionFolderName ' + this.templatedata.projectname;
var generateBuildConfigs = this.settings.generateBuildConfigs.toLowerCase == 'true';
const scriptParameters = '-SolutionFile \'' + this.destinationPath(SolutionFile) + '\' -Name ' + this.settings.LayerPrefixedProjectName + ' -Type ' + this.layer + ' -ProjectPath \'' + this.settings.ProjectPath + '\'' + ' -SolutionFolderName \'' + this.templatedata.projectname + '\'' + ' -GenerateBuildConfigs ' + `$${this.settings.generateBuildConfigs}`;

var pathToAddProjectScript = path.join(this._sourceRoot, '../../../powershell/add-project.ps1');
powershell.runAsync(pathToAddProjectScript, scriptParameters);
}

end() {
if(fs.existsSync(this.destinationPath('helix-template/_project.csproj'))){
this._renameProjectFile();
}
this._renameProjectFile();
}
};
8 changes: 6 additions & 2 deletions powershell/Add-Project.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ param(
[Parameter(Mandatory=$true)]
[string]$ProjectPath,
[Parameter(Mandatory=$true)]
[string]$SolutionFolderName)
[string]$SolutionFolderName,
[Parameter(Mandatory=$true)]
[bool]$GenerateBuildConfigs)

. $PSScriptRoot\Add-Line.ps1
. $PSScriptRoot\Get-SolutionConfigurations.ps1
Expand All @@ -36,7 +38,9 @@ $addProjectSolutionFolder = @("Project(`"{2150E333-8FDC-42A3-9474-1A3956D46DE8}`
$addNestProjectSection = @("`t`t{$projectGuid} = {$projectFolderGuid}")
$addNestProjectSolutionFolderSection = @("`t`t{$projectFolderGuid} = $solutionFolderId")

Add-BuildConfigurations -ProjectPath $projectPath -Configurations $configurations
if ($GenerateBuildConfigs) {
Add-BuildConfigurations -ProjectPath $projectPath -Configurations $configurations
}
Add-Line -FileName $SolutionFile -Pattern $ProjectSection -LinesToAdd $addProjectSection
Add-Line -FileName $SolutionFile -Pattern $ProjectSection -LinesToAdd $addProjectSolutionFolder
Add-Line -FileName $SolutionFile -Pattern $NestedProjectSection -LinesToAdd $addNestProjectSection
Expand Down
2 changes: 1 addition & 1 deletion powershell/Get-SolutionFolderId.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Function Get-SolutionFolderId {
$msbuildPath = Get-MSBuildPath
Add-Type -Path "$msbuildPath\Microsoft.Build.dll"
$solution = [Microsoft.Build.Construction.SolutionFile]::Parse($SolutionFile)
$solution.ProjectsInOrder| where-object {$_.ProjectType -eq [Microsoft.Build.Construction.SolutionProjectType]::SolutionFolder } | where-object {$_.ProjectName -like "*$type*"} | Select-Object -ExpandProperty ProjectGuid
$solution.ProjectsInOrder| where-object {$_.ProjectType -eq [Microsoft.Build.Construction.SolutionProjectType]::SolutionFolder } | where-object {$_.ProjectName -like "*$type*"} | Select-Object -Index 0 | Select-Object -ExpandProperty ProjectGuid
}