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

BugZilla 518578 : Let users to use .Chefile in addition to Chefile #5471

Merged
merged 3 commits into from
Jul 18, 2017
Merged
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
80 changes: 71 additions & 9 deletions dockerfiles/lib/src/internal/dir/che-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ export class CheDir {
currentFolder: string = this.path.resolve('./');
folderName: any;
cheFile : any;

/**
* Alternate path to the Chefile file with .Chefile
*/
dotCheFile: string;

/**
* Alternate path to the Chefile file with .chefile
*/
dotCheFileLowercase: string;

dotCheFolder : any;
workspacesFolder : any;
cliFolder : any;
Expand Down Expand Up @@ -100,6 +111,8 @@ export class CheDir {
this.currentFolder = this.path.resolve(args[0]);
this.folderName = this.path.basename(this.currentFolder);
this.cheFile = this.path.resolve(this.currentFolder, 'Chefile');
this.dotCheFile = this.path.resolve(this.currentFolder, '.Chefile');
this.dotCheFileLowercase = this.path.resolve(this.currentFolder, '.chefile');
this.dotCheFolder = this.path.resolve(this.currentFolder, '.che');
this.dotCheIdFile = this.path.resolve(this.dotCheFolder, 'id');
this.dotCheSshPrivateKeyFile = this.path.resolve(this.dotCheFolder, 'ssh-key.private');
Expand Down Expand Up @@ -197,12 +210,16 @@ export class CheDir {

parse() {

try {
this.fs.statSync(this.cheFile);
// we have a file
} catch (e) {
let cheFiles : Array<any> = this.findCheFiles();

if (cheFiles.length === 0) {
Log.getLogger().debug('No chefile defined, use default settings');
return;
} else if (cheFiles.length >= 2) {
throw new Error('Error while parsing the Chefile as it was found at ' + JSON.stringify(cheFiles) + '. Only one file at a time is allowed.');
} else {
this.cheFile = cheFiles[0];
Log.getLogger().debug('Parsing chefile with name ', this.cheFile);
}

// load the chefile script if defined
Expand Down Expand Up @@ -366,25 +383,70 @@ export class CheDir {

}

/**
* Checks if the given file is existing or not
* @param filename the path to check
* @returns {boolean} true if it exists
*/
fileExistsCaseSensitive(filename) : boolean {
var dir = this.path.dirname(filename);
if (dir === '/' || dir === '.') {
return true;
}
var filenames = this.fs.readdirSync(dir);
if (filenames.indexOf(this.path.basename(filename)) === -1) {
return false;
}
return this.fileExistsCaseSensitive(dir);
}

/**
* Search all the chefiles with the corresponding pattern : Chefile, .Chefile, .chefile
* @returns {Array<any>} containing all references found
*/
findCheFiles() : Array<any> {

let foundCheFiles : Array<any> = new Array<any>();

// Try with Chefile
if (this.fileExistsCaseSensitive(this.cheFile)) {
Log.getLogger().debug('Chefile is present at ', this.cheFile);
foundCheFiles.push(this.cheFile);
}

// Try with .Chefile
if (this.fileExistsCaseSensitive(this.dotCheFile)) {
Log.getLogger().debug('The alternate file .Chefile is present at ', this.dotCheFile);
foundCheFiles.push(this.dotCheFile);
}

// Try with .chefile
if (this.fileExistsCaseSensitive(this.dotCheFileLowercase)) {
this.fs.statSync(this.dotCheFileLowercase);
Log.getLogger().debug('The alternate file .chefile is present at ', this.dotCheFileLowercase);
foundCheFiles.push(this.dotCheFileLowercase);
}
return foundCheFiles;
}


init() : Promise<any> {
return this.isInitialized().then((isInitialized) => {
if (isInitialized) {
Log.getLogger().warn('Che already initialized');
} else {
// needs to create folders
Log.getLogger().info('Adding', this.dotCheFolder, 'directory');
this.initCheFolders();

let cheFiles: Array<any> = this.findCheFiles();

// write a default chefile if there is none
try {
this.fs.statSync(this.cheFile);
Log.getLogger().debug('Chefile is present at ', this.cheFile);
} catch (e) {
if (cheFiles.length === 0) {
// write default
Log.getLogger().debug('Write a default Chefile at ', this.cheFile);
this.writeDefaultChefile();
}
Log.getLogger().info('Adding', this.dotCheFolder, 'directory');
return true;
}

Expand Down