Skip to content

Commit

Permalink
Merge pull request #110 from Springrbua/master
Browse files Browse the repository at this point in the history
Changed AngularCLI Global preferences and CLI setting in Angular Proj…
  • Loading branch information
angelozerr authored Sep 14, 2017
2 parents d8c9a22 + 653c544 commit 73b8245
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public class AngularCLIMessages extends NLS {
public static String NewAngularProjectWizard_unsupportedProjectNames;

public static String WizardNewNgProjectCreationPage_angular_cli_group_label;
public static String WizardNewNgProjectCreationPage_useGlobalAngularCLI;
public static String WizardNewNgProjectCreationPage_useGlobalPreferencesCLI;
public static String WizardNewNgProjectCreationPage_useInstallAngularCLI;
public static String WizardNewNgProjectCreationPage_searchingForGlobalAngularCLI;
public static String WizardNewNgProjectCreationPage_noGlobalAngularCLI;
public static String WizardNewNgProjectCreationPage_searchingForGlobalPreferencesCLI;
public static String WizardNewNgProjectCreationPage_noGlobalPreferencesCLI;

public static String NewAngularProjectParamsWizardPage_title;
public static String NewAngularProjectParamsWizardPage_prefix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ NewAngularProjectWizard_invalidProjectName=Project name "{0}" is not valid. New
NewAngularProjectWizard_unsupportedProjectNames=Project name "{0}" is not a supported name.

WizardNewNgProjectCreationPage_angular_cli_group_label=Angular CLI
WizardNewNgProjectCreationPage_useGlobalAngularCLI=Use global
WizardNewNgProjectCreationPage_useGlobalPreferencesCLI=Use global preferences
WizardNewNgProjectCreationPage_useInstallAngularCLI=Install
WizardNewNgProjectCreationPage_searchingForGlobalAngularCLI=Searching for global Angular CLI...
WizardNewNgProjectCreationPage_noGlobalAngularCLI=No global Angular CLI installation found.
WizardNewNgProjectCreationPage_searchingForGlobalPreferencesCLI=Searching for global Angular CLI in global preferences...
WizardNewNgProjectCreationPage_noGlobalPreferencesCLI=No Angular CLI installation found in global preferences.

NewAngularProjectParamsWizardPage_title=Optional Params
NewAngularProjectParamsWizardPage_prefix=&Prefix:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import ts.eclipse.ide.ui.preferences.BrowseButtonsComposite;
import ts.eclipse.ide.ui.preferences.OptionsConfigurationBlock;
import ts.eclipse.ide.ui.preferences.ScrolledPageContent;
import ts.eclipse.ide.ui.preferences.StatusInfo;
import ts.eclipse.ide.ui.widgets.IStatusChangeListener;
import ts.utils.FileUtils;
import ts.utils.StringUtils;
Expand Down Expand Up @@ -74,7 +75,6 @@ public class AngularCLIConfigurationBlock extends OptionsConfigurationBlock {
private Text cliPath;
private Text cliVersion;
private NgVersionJob ngVersionJob;
private File ngFile;

public AngularCLIConfigurationBlock(IStatusChangeListener context, IProject project,
IWorkbenchPreferenceContainer container) {
Expand Down Expand Up @@ -255,13 +255,12 @@ private String[] getDefaultPaths() {
protected void validateSettings(Key changedKey, String oldValue, String newValue) {
cliVersion.setText("");
cliPath.setText("");
ngFile = null;
ngVersionJob.cancel();
CLIStatus status = validateCLIPath();
if (status.isOK()) {
cliPath.setText(FileUtils.getPath(status.getNgFile()));
IStatus status = validateCLIPath();
if (status.isOK() && status instanceof CLIStatus) {
File ngFile = ((CLIStatus)status).getNgFile();
cliPath.setText(FileUtils.getPath(ngFile));
cliVersion.setText("Executing 'ng --version'...");
ngFile = status.getNgFile();
ngVersionJob.setNgFile(ngFile);
ngVersionJob.schedule();
} else {
Expand All @@ -274,17 +273,18 @@ protected void validateSettings(Key changedKey, String oldValue, String newValue
*
* @return the status of the ng path.
*/
private CLIStatus validateCLIPath() {
private IStatus validateCLIPath() {
File ngFile = null;
boolean useGlobal = ngUseGlobalInstallation.getSelection();
boolean globalPrefs = getProject() == null;
if (useGlobal) {
ngFile = CLIProcessHelper.findNg();
if (ngFile == null) {
// ERROR: ng was not installed with "npm install @angular/cli
// -g"
return new CLIStatus(null, AngularCLIMessages.AngularCLIConfigurationBlock_ngGlobal_notFound_error);
}
} else {
} else if (!globalPrefs) {
String ngPath = ngCustomFilePath.getText();
if (StringUtils.isEmpty(ngPath)) {
// ERROR: the installed path is empty
Expand All @@ -301,14 +301,16 @@ private CLIStatus validateCLIPath() {
ngFile = new File(ngFile, CLIProcessHelper.getNgFileName());
}
}

if (!ngFile.exists()) {
// ERROR: ng file doesn't exists
return new CLIStatus(null,
NLS.bind(AngularCLIMessages.AngularCLIConfigurationBlock_ngCustomFile_exists_error,
FileUtils.getPath(ngFile)));
} else {

if (!globalPrefs) {
if (!ngFile.exists()) {
// ERROR: ng file doesn't exists
return new CLIStatus(null,
NLS.bind(AngularCLIMessages.AngularCLIConfigurationBlock_ngCustomFile_exists_error,
FileUtils.getPath(ngFile)));
}
}
else {
return StatusInfo.OK_STATUS;
}
// ng path is valid
return new CLIStatus(ngFile, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
Expand Down Expand Up @@ -42,6 +43,9 @@
import ts.eclipse.ide.angular.cli.utils.CLIStatus;
import ts.eclipse.ide.angular.cli.utils.NgVersionJob;
import ts.eclipse.ide.angular.internal.cli.AngularCLIMessages;
import ts.eclipse.ide.angular.internal.cli.AngularCLIProject;
import ts.eclipse.ide.angular.internal.cli.AngularCLIProjectSettings;
import ts.eclipse.ide.angular.internal.cli.Trace;
import ts.eclipse.ide.core.utils.WorkbenchResourceUtil;
import ts.eclipse.ide.terminal.interpreter.EnvPath;
import ts.eclipse.ide.terminal.interpreter.LineCommand;
Expand All @@ -63,12 +67,12 @@ public class WizardNewNgProjectCreationPage extends AbstractWizardNewTypeScriptP
"app" };

// Angular CLI
private Boolean hasGlobalAngularCLI;
private Button useGlobalAngularCLIButton;
private Boolean hasGlobalPreferencesCLI;
private Button useGlobalCLIPreferencesButton;
private Button useInstallAngularCLIButton;
private Text globalAngularCLIVersion;
private NpmInstallWidget installAngularCLI;
private boolean useGlobalAngularCLI;
private boolean useGlobalCLIPreferences;

private NgVersionJob ngVersionJob;

Expand Down Expand Up @@ -98,10 +102,10 @@ protected void createPageBody(Composite parent) {

/** Creates the field for install Angular CLI. */
private void createGlobalAngularCLIField(Composite parent) {
useGlobalAngularCLIButton = new Button(parent, SWT.RADIO);
useGlobalAngularCLIButton.setText(AngularCLIMessages.WizardNewNgProjectCreationPage_useGlobalAngularCLI);
useGlobalAngularCLIButton.addListener(SWT.Selection, this);
useGlobalAngularCLIButton.addSelectionListener(new SelectionAdapter() {
useGlobalCLIPreferencesButton = new Button(parent, SWT.RADIO);
useGlobalCLIPreferencesButton.setText(AngularCLIMessages.WizardNewNgProjectCreationPage_useGlobalPreferencesCLI);
useGlobalCLIPreferencesButton.addListener(SWT.Selection, this);
useGlobalCLIPreferencesButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateAngularCLIMode();
Expand Down Expand Up @@ -134,56 +138,72 @@ public void statusChanged(IStatus status) {
@Override
protected void initializeDefaultValues() {
super.initializeDefaultValues();
useGlobalAngularCLIButton.setSelection(true);
useGlobalCLIPreferencesButton.setSelection(true);
}

@Override
protected void nodeJsChanged(File nodeFile) {
super.nodeJsChanged(nodeFile);
if (hasGlobalAngularCLI == null && nodeFile != null && ngVersionJob == null) {
ngVersionJob = new NgVersionJob();
ngVersionJob.setNodeFile(nodeFile);
ngVersionJob.setNgFile(CLIProcessHelper.findNg());
ngVersionJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
super.done(event);
IStatus status = event.getResult();
final String version;
if (status instanceof CLIStatus) {
final CLIStatus s = (CLIStatus) status;
version = s.getVersion();
if (s.isOK())
hasGlobalAngularCLI = Boolean.TRUE;
} else {
hasGlobalAngularCLI = Boolean.FALSE;
version = "";
}
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
if (!globalAngularCLIVersion.isDisposed())
globalAngularCLIVersion.setText(version);
validatePage();
if (hasGlobalPreferencesCLI == null && nodeFile != null && ngVersionJob == null) {
File ngFile = null;
try {
AngularCLIProjectSettings settings = AngularCLIProject.getAngularCLIProject(getProjectHandle()).getSettings();
ngFile = settings.getNgFile();
if (ngFile != null && ngFile.isDirectory())
ngFile = new File(ngFile, CLIProcessHelper.getNgFileName());
else
ngFile = null;
} catch (CoreException e) {
Trace.trace(Trace.SEVERE, "Error while getting Project settings", e);
ngFile = null;
}
if (ngFile == null || !ngFile.exists())
hasGlobalPreferencesCLI = Boolean.FALSE;
else {
ngVersionJob = new NgVersionJob();
ngVersionJob.setNodeFile(nodeFile);
ngVersionJob.setNgFile(ngFile);
ngVersionJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
super.done(event);
IStatus status = event.getResult();
final String version;
if (status instanceof CLIStatus) {
final CLIStatus s = (CLIStatus) status;
version = s.getVersion();
if (s.isOK())
hasGlobalPreferencesCLI = Boolean.TRUE;
} else {
hasGlobalPreferencesCLI = Boolean.FALSE;
version = "";
}
});
}
});
ngVersionJob.schedule();
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
if (!globalAngularCLIVersion.isDisposed())
globalAngularCLIVersion.setText(version);
validatePage();
}
});
}
});
ngVersionJob.schedule();
}
}
}

@Override
protected void updateComponents(Event event) {
super.updateComponents(event);
Widget item = event != null ? event.item : null;
if (item == null || item == useGlobalAngularCLIButton)
if (item == null || item == useGlobalCLIPreferencesButton)
updateAngularCLIMode();
}

private void updateAngularCLIMode() {
useGlobalAngularCLI = useGlobalAngularCLIButton.getSelection();
installAngularCLI.setEnabled(!useGlobalAngularCLI);
useGlobalCLIPreferences = useGlobalCLIPreferencesButton.getSelection();
installAngularCLI.setEnabled(!useGlobalCLIPreferences);
}

@Override
Expand Down Expand Up @@ -217,13 +237,13 @@ private IStatus validateNgProjectName(String name) {

/** Validates the Angular CLI. */
private IStatus validateAngularCLI() {
if (useGlobalAngularCLI) {
if (hasGlobalAngularCLI == null)
if (useGlobalCLIPreferences) {
if (hasGlobalPreferencesCLI == null)
return new Status(IStatus.ERROR, AngularCLIPlugin.PLUGIN_ID,
AngularCLIMessages.WizardNewNgProjectCreationPage_searchingForGlobalAngularCLI);
else if (!hasGlobalAngularCLI.booleanValue())
AngularCLIMessages.WizardNewNgProjectCreationPage_searchingForGlobalPreferencesCLI);
else if (!hasGlobalPreferencesCLI.booleanValue())
return new Status(IStatus.ERROR, AngularCLIPlugin.PLUGIN_ID,
AngularCLIMessages.WizardNewNgProjectCreationPage_noGlobalAngularCLI);
AngularCLIMessages.WizardNewNgProjectCreationPage_noGlobalPreferencesCLI);
else
return Status.OK_STATUS;
}
Expand All @@ -232,7 +252,7 @@ else if (!hasGlobalAngularCLI.booleanValue())

@Override
public void updateCommand(List<LineCommand> commands, final IProject project, String nodeFilePath) {
if (!useGlobalAngularCLI) {
if (!useGlobalCLIPreferences) {
// when Angular CLI is installed, update the project Eclipse preferences
// to consume this installed Angular CLI.
commands.add(new LineCommand(installAngularCLI.getNpmInstallCommand(), new TerminalCommandAdapter() {
Expand All @@ -257,7 +277,7 @@ public void run() {
}
}));
commands.add(EnvPath.createSetPathCommand(EnvPath.insertToEnvPath(nodeFilePath, FileUtils
.getPath(WorkbenchResourceUtil.resolvePath("${project_loc:node_modules/.bin}", project)))));
.getPath(WorkbenchResourceUtil.resolvePath("${project_loc:node_modules/.bin}", project)))));
}
}

Expand Down

0 comments on commit 73b8245

Please sign in to comment.