Skip to content
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
31 changes: 29 additions & 2 deletions src/main/java/greed/Greed.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.swing.JPanel;

Expand Down Expand Up @@ -157,7 +159,7 @@ public void generateCode(boolean regen) {

private String renderedCodeRoot(GreedConfig config)
{
return currentEngine.render(config.getCodeRoot(), currentModel);
return currentEngine.render(config.getCodeRoot(), currentModel).replace(" ", "");
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -337,7 +339,7 @@ else if (!templateSet.contains(depTemplate)) {
}

indivModel.put("GeneratedFileName", new java.io.File(filePath).getName());
indivModel.put("GeneratedFilePath", FileSystem.getRawFile(filePath).getPath());
indivModel.put("GeneratedFilePath", FileSystem.getRawFile(filePath).getPath().replace("\\", "/"));

boolean exists = FileSystem.exists(filePath);
TemplateConfig.OverwriteOptions overwrite = template.getOverwrite();
Expand Down Expand Up @@ -375,6 +377,15 @@ else if (!templateSet.contains(depTemplate)) {
FileSystem.writeFile(filePath, output);
}

if (filePath.endsWith("html")) {
try {
Runtime.getRuntime().exec("open " + FileSystem.getRawFile(filePath).getPath().replace("\\", "/"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if (template.getAfterFileGen() != null) {
CommandConfig afterGen = template.getAfterFileGen();
String[] commands = new String[afterGen.getArguments().length + 1];
Expand All @@ -398,10 +409,26 @@ else if (!templateSet.contains(depTemplate)) {
talkingWindow.showLine("");
}

clionSet(problem.getClassName());

talkingWindow.showLine("All set, good luck!");
talkingWindow.showLine("");
}

//clion 工程设置
private void clionSet(String fileName) {
GreedConfig config = Utils.getGreedConfig();
InputStream cmakeTemplate = getClass().getResourceAsStream("/templates/clion/CMakeLists.tmpl");

Map<String, Object> model = new HashMap<String, Object>();

model.put("FileName", renderedCodeRoot(config) + "/" + fileName);

String cmake = currentEngine.render(cmakeTemplate, model);
FileSystem.writeFile("CMakeLists.txt", cmake);

}

private HashMap<String, Object> mergeModels(HashMap<String, Object> ... models) {
HashMap<String, Object> merged = new HashMap<String, Object>();
for (HashMap<String, Object> model: models)
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/greed/code/lang/CppLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public String renderPrimitive(Primitive primitive) {
return "int";
case BOOL:
return "bool";
case CHAR:
return "char";
case LONG:
// TODO: Bad practice, need decoupling
return Utils.getGreedConfig().getLanguage().get(Language.getName(Language.CPP)).getLongIntTypeName();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/greed/model/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public static Type convertType(DataType dt) {
if ("long".equals(typeName)) type = Primitive.LONG;
if ("double".equals(typeName)) type = Primitive.DOUBLE;
if ("bool".equals(typeName)) type = Primitive.BOOL;
if ("char".equals(typeName)) type = Primitive.CHAR;
return new Type(type, dt.getDimension());
}
}
2 changes: 1 addition & 1 deletion src/main/java/greed/model/Primitive.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* Greed is good! Cheers!
*/
public enum Primitive {
INT, LONG, STRING, BOOL, DOUBLE
INT, LONG, STRING, BOOL, DOUBLE, CHAR
}
3 changes: 3 additions & 0 deletions src/main/java/greed/model/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static Type primitiveType(Primitive primitive) {
case STRING: return STRING_TYPE;
case LONG: return LONG_TYPE;
case DOUBLE: return DOUBLE_TYPE;
case CHAR:
return CHAR_TYPE;
}
throw new IllegalArgumentException("Unknown primitive : " + primitive);
}
Expand All @@ -30,6 +32,7 @@ public static Type primitiveType(Primitive primitive) {
public static final Type LONG_TYPE = new Type(Primitive.LONG, 0);
public static final Type DOUBLE_TYPE = new Type(Primitive.DOUBLE, 0);
public static final Type BOOL_TYPE = new Type(Primitive.BOOL, 0);
public static final Type CHAR_TYPE = new Type(Primitive.CHAR, 0);

public static final Type INT_ARRAY_TYPE = new Type(Primitive.INT, 1);
public static final Type STRING_ARRAY_TYPE = new Type(Primitive.STRING, 1);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/greed/template/StringUtilRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ private String applyUnquote(String s) {
int n = s.length();
if (n >= 2 && s.charAt(0) == '"' && s.charAt(n - 1) == '"')
s = s.substring(1, n - 1);
else if (n >= 2 && s.charAt(0) == '\'' && s.charAt(n - 1) == '\'')
s = s.substring(1, n - 1);
return s;
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/templates/clion/CMakeLists.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.3)
project(topcoder)

set(CMAKE_CXX_FLAGS "\${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES "${FileName}")
add_executable(topcoder \${SOURCE_FILES})
2 changes: 1 addition & 1 deletion src/main/resources/templates/filetest/cpp.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ifstream data("${Dependencies.testcase.GeneratedFileName}");
ifstream data("${Dependencies.testcase.GeneratedFilePath}");

string next_line() {
string s;
Expand Down
23 changes: 9 additions & 14 deletions src/main/resources/templates/source/cpp.tmpl
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FT(i,f,t) for(int i=f;i<=t;++i)
#define TF(i,t,f) for(int i=t;i>=f;--i)
#define REP(i,n) for(int i=0;i<n;++i)
#define MM(a,v) memset(a,v,sizeof(a))
#define ITR(it,ct) for(auto it=ct.begin();it!=ct.end();++it)
#define MOD 1000000007

class ${ClassName} {
public:
struct ${ClassName} {
${Method.ReturnType} ${Method.Name}(${Method.Params}) {
return ${Method.ReturnType;zeroval};
}
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/greed/template/ClionTemplateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package greed.template;

import greed.conf.ConfigException;
import greed.model.Language;
import greed.util.Utils;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

public class ClionTemplateTest {

private InputStream cmakeTemplate;

Map<String, Object> model = new HashMap<String, Object>();
TemplateEngine engine;

@BeforeClass
public static void initializeGreed() throws ConfigException {
// TODO : Why at all do we need this?
Utils.initialize();
}

@Before
public void setupTemplates() throws IOException {
this.cmakeTemplate = getClass().getResourceAsStream("/templates/clion/CMakeLists.tmpl");
assertThat(this.cmakeTemplate, notNullValue());

engine = TemplateEngine.newLanguageEngine(Language.CPP);
}

@Test
public void renderCppCode() {
model.put("GeneratedFilePath", "GeneratedFilePath");
model.put("GeneratedFileName", ".GeneratedFileName");
String cmake = engine.render(cmakeTemplate, model);
System.out.println(cmake);
}
}