Skip to content

Commit

Permalink
Merge pull request #12 from suparngp/refactoring
Browse files Browse the repository at this point in the history
2.1.0
  • Loading branch information
Lukasz Karolewski committed Dec 9, 2015
2 parents 25d3c16 + b183525 commit 569c90d
Show file tree
Hide file tree
Showing 26 changed files with 483 additions and 485 deletions.
7 changes: 4 additions & 3 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion META-INF/plugin.xml
Expand Up @@ -24,6 +24,11 @@

<change-notes>
<![CDATA[
<h3>12/08/2015</h3>
<ol>
<li>fixed file naming issues</li>
</ol>
<h3>4/17/2015</h3>
<ol>
<li>fixed filter unit test stub</li>
Expand Down Expand Up @@ -54,6 +59,8 @@
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<applicationService serviceImplementation="org.weebly.generator.services.ConfigurationLoader"/>
<applicationService serviceImplementation="org.weebly.generator.services.ContentBuilder"/>
<applicationService serviceImplementation="org.weebly.generator.services.FileHandler"/>
<applicationService serviceImplementation="org.weebly.generator.services.TemplateLoader"/>
</extensions>

Expand All @@ -68,7 +75,7 @@
<actions>
<!-- Add your actions here -->
<action id="angularComponent" class="org.weebly.generator.actions.CreateAction" text="Angular Component"
description="Generate a component for angular" icon="/icons/action.png">
description="Generate angular component" icon="/icons/action.png">
<add-to-group group-id="NewGroup" anchor="first"/>
</action>
</actions>
Expand Down
2 changes: 1 addition & 1 deletion files/templates/Controller.js
Expand Up @@ -3,7 +3,7 @@
* @name #MODULENAME#:#COMPONENTNAME#
*
* @description
* _Please update the description and dependencies._
*
*
* @requires $scope
* */
Expand Down
2 changes: 1 addition & 1 deletion files/templates/Directive.js
Expand Up @@ -3,7 +3,7 @@
* @name #MODULENAME#:#COMPONENTNAME#
*
* @description
* _Please update the description and restriction._
*
*
* @restrict A
* */
Expand Down
2 changes: 1 addition & 1 deletion files/templates/DirectiveSpec.js
Expand Up @@ -6,7 +6,7 @@ describe('Directive: #MODULENAME#.#COMPONENTNAME#', function () {
scope = $rootScope.$new();

//update the line below!
ele = angular.element('<div #COMPONENTNAME# ></div>');
ele = angular.element('<div #COMPONENTNAME#></div>');
//update the above line if you haven't!
$compile(ele)(scope);
scope.$apply();
Expand Down
3 changes: 2 additions & 1 deletion files/templates/Factory.js
@@ -1,8 +1,9 @@
/**
* @ngdoc service
* @name #MODULENAME#:#COMPONENTNAME#
*
* @description
* _Please update the description and dependencies._
*
*
* */
angular.module('#MODULENAME#')
Expand Down
3 changes: 2 additions & 1 deletion files/templates/Filter.js
@@ -1,8 +1,9 @@
/**
* @ngdoc service
* @name #MODULENAME#:#COMPONENTNAME#
*
* @description
* _Please update the description and dependencies._
*
*
* */
angular.module('#MODULENAME#')
Expand Down
2 changes: 1 addition & 1 deletion files/templates/Module.js
Expand Up @@ -3,7 +3,7 @@
* @name #MODULENAME#
*
* @description
* _Please update the description and dependencies._
*
*
* */
angular.module('#MODULENAME#', [])
Expand Down
5 changes: 3 additions & 2 deletions files/templates/Service.js
@@ -1,16 +1,17 @@
/**
* @ngdoc service
* @name #MODULENAME#:#COMPONENTNAME#
*
* @description
* _Please update the description and dependencies._
*
*
* */
angular.module('#MODULENAME#')
.service('#COMPONENTNAME#', function(){

this.testMethod = function() {

}
};

});

151 changes: 7 additions & 144 deletions src/org/weebly/generator/actions/CreateAction.java
Expand Up @@ -5,170 +5,33 @@
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import org.weebly.generator.model.Component;
import org.weebly.generator.services.Configuration;
import org.weebly.generator.services.ConfigurationLoader;
import org.weebly.generator.services.FileHandler;
import org.weebly.generator.actions.dialogs.CreateFile;
import org.weebly.generator.services.TemplateLoader;
import org.weebly.generator.model.exceptions.AngularIUnitException;
import org.weebly.generator.forms.CreateFile;
import org.weebly.generator.forms.ErrorDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CreateAction extends AnAction {
private static Project project;

private static TemplateLoader templateLoader;
private static ConfigurationLoader configurationLoader;

private static String currentPath = "";
private static AnActionEvent e;

public CreateAction() {
templateLoader = ServiceManager.getService(TemplateLoader.class);
configurationLoader = ServiceManager.getService(ConfigurationLoader.class);

if(configurationLoader.getState() == null){
configurationLoader.loadState(new Configuration());
}
}

public void actionPerformed(AnActionEvent _e_) {
e = _e_;
public void actionPerformed(AnActionEvent e) {
Project project = e.getData(PlatformDataKeys.PROJECT);

VirtualFile data = e.getData(PlatformDataKeys.VIRTUAL_FILE);
String currentPath = data.getPath();

if(data == null){
showError("Unknown Error", "Please file an issue if you think this is a bug");
return;
}

currentPath = data.getPath();
project = e.getData(PlatformDataKeys.PROJECT);

new CreateFile(this, templateLoader).showDialog();
new CreateFile(project, currentPath, templateLoader.getComponentTypes()).showDialog();
}

@Override
public void update(AnActionEvent e) {
super.update(e);
VirtualFile file = e.getData(PlatformDataKeys.VIRTUAL_FILE);
if(file == null || !file.isDirectory()){
e.getPresentation().setEnabled(false);
}
else{
e.getPresentation().setEnabled(true);
}
}

/**
* Handles the callback from the UI to create the component
* @param componentName name of component
* @param componentType type of component
* @param moduleName name of module
*/
public void createAngularComponentsHandler(String componentName, String componentType, String moduleName) {

try {
String codeFileName = templateLoader.getSrcFilename(componentName, componentType);
String specFileName = templateLoader.getTestFilename(componentName, componentType);

File codeFile = FileHandler.createFile(codeFileName, currentPath);
File specFile = FileHandler.createFile(specFileName, currentPath);

File currentDirectory = new File(currentPath);

VirtualFile fileByIoFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(currentDirectory);

if (fileByIoFile != null) {
fileByIoFile.getChildren();
fileByIoFile.refresh(false, true);

Component tmp = templateLoader.getComponents().get(componentType);

FileHandler.writeFileContent(codeFile, templateLoader.formatContent(tmp.code, componentName, moduleName));
FileHandler.writeFileContent(specFile, templateLoader.formatContent(tmp.spec, componentName, moduleName));

//open the file after writing content to it.
fileByIoFile.refresh(false, true);
fileByIoFile.getFileSystem().refresh(false);
openFileInEditor(specFileName);
openFileInEditor(codeFileName);

//since all the files have been created successfully, add the data to persistent state.
persistModuleName(moduleName, codeFileName, specFileName);

} else {
System.out.println("File not refreshed");
}

} catch (AngularIUnitException ae) {
showError(ae.getName(), ae.getDescription());
} catch (Exception e) {
e.printStackTrace();
showError("Unknown Error", "Please report in case you think that this is a bug.");
}
}



private void persistModuleName(String moduleName, String codeFileName, String specFileName) {
Configuration state = configurationLoader.getState();
if(state != null){
if(state.getMainSpecFilesMap() == null){
state.setMainSpecFilesMap(new HashMap<String, String>());
}
state.getMainSpecFilesMap().put(codeFileName, currentPath + "/" + specFileName);

if(state.getModuleNameSuggestions() == null){
state.setModuleNameSuggestions(new ArrayList<String>());
}
if(!state.getModuleNameSuggestions().contains(moduleName)){
state.getModuleNameSuggestions().add(moduleName);
}
}
}

private void openFileInEditor(String filename) {
Commons.openFileInEditor(project, currentPath + "/" + filename);
}



/**
* Displays the error dialog
*
* @param title the title of the error
* @param description the description of the error
*/
private void showError(String title, String description) {
new ErrorDialog(title, description).display();
}

/**
* Checks if the main JS file and test file exists
*
* @param fileName the filename
* @return true if the templates don't exist, otherwise false
*/
public boolean checkIfFileExists(String fileName) {
return FileHandler.fileExists(currentPath + "/" + fileName);
}

/**
* Returns the suggestions for the module names
*
* @return the list of module names used so far
*/
public List<String> getModuleNameSuggestions() {
return configurationLoader.getState() == null
? new ArrayList<String>()
: configurationLoader.getState().getModuleNameSuggestions();
//display only if clicked on directory
e.getPresentation().setEnabled(file != null && file.isDirectory());
}
}

0 comments on commit 569c90d

Please sign in to comment.