Skip to content

Commit

Permalink
Add a project configuration session
Browse files Browse the repository at this point in the history
  • Loading branch information
ngallagher committed Nov 20, 2017
1 parent 59ec3aa commit ca6148e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package org.snapscript.studio.index;
import static org.snapscript.core.Reserved.IMPORT_FILE;

import java.io.File;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -14,7 +12,6 @@
import java.util.regex.Pattern;

import org.snapscript.core.Context;
import org.snapscript.core.link.ImportPathResolver;
import org.snapscript.studio.common.FileAction;
import org.snapscript.studio.common.FileProcessor;
import org.snapscript.studio.common.FileReader;
Expand All @@ -26,7 +23,6 @@ public class IndexScanner implements IndexDatabase {
private final FileProcessor<IndexFile> processor;
private final FileAction<IndexFile> action;
private final IndexPathTranslator translator;
private final ImportPathResolver resolver;
private final ClassPathSearcher searcher;
private final Indexer indexer;
private final String project;
Expand All @@ -38,7 +34,6 @@ public IndexScanner(ClassLoader loader, Context context, Executor executor, File
this.indexer = new Indexer(translator, this, context, executor, root);
this.action = new CompileAction(indexer, root);
this.processor = new FileProcessor<IndexFile>(action, executor);
this.resolver = new ImportPathResolver(IMPORT_FILE);
this.project = project;
this.root = root;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;

import org.snapscript.common.store.NotFoundException;
import org.snapscript.common.store.Store;
Expand All @@ -32,7 +31,8 @@

public class Project implements FileDirectory {

private final AtomicReference<IndexDatabase> reference;
private static final String INDEX_DATABASE = "database";

private final ConfigurationClassLoader classLoader;
private final ConfigurationReader reader;
private final ProjectFileSystem fileSystem;
Expand All @@ -42,7 +42,6 @@ public class Project implements FileDirectory {
private final Store store;

public Project(ConfigurationReader reader, Workspace workspace, String projectDirectory, String projectName) {
this.reference = new AtomicReference<IndexDatabase>();
this.classLoader = new ConfigurationClassLoader(this);
this.fileSystem = new ProjectFileSystem(this);
this.store = new ProjectStore();
Expand Down Expand Up @@ -102,20 +101,20 @@ public Context getProjectContext() {
}
}

public synchronized IndexDatabase getIndexDatabase(){
IndexDatabase database = reference.get();
public IndexDatabase getIndexDatabase(){
ProjectConfiguration configuraton = getConfiguration();
IndexDatabase database = configuraton.getAttribute(INDEX_DATABASE);

if(database == null) {
IndexScanner indexScanner = new IndexScanner(
database = new IndexScanner(
getClassLoader(),
getProjectContext(),
getWorkspace().getExecutor(),
getSourcePath(),
getProjectName(),
getLayout().getPrefixes());

reference.set(indexScanner);
return indexScanner;
configuraton.setAttribute(INDEX_DATABASE, database);
}
return database;
}
Expand All @@ -132,10 +131,18 @@ public String getScriptPath(String resource) {
return getLayout().getDownloadPath(getSourcePath(), resource);
}

public ProjectConfiguration getConfiguration() {
try {
return reader.loadProjectConfiguration(projectName);
} catch (Exception e) {
workspace.getLogger().info("Could not read .project file for '" + projectName + "'", e);
}
return null;
}

public ProjectLayout getLayout() {
try {
ProjectConfiguration configuration = reader.loadProjectConfiguration(projectName);
return configuration.getProjectLayout();
return getConfiguration().getProjectLayout();
} catch (Exception e) {
workspace.getLogger().info("Could not read .project file for '" + projectName + "'", e);
}
Expand Down Expand Up @@ -226,7 +233,7 @@ private String createClassPath() throws Exception {

private List<File> resolveDependencies(){
try {
ProjectConfiguration configuration = reader.loadProjectConfiguration(projectName);
ProjectConfiguration configuration = getConfiguration();
List<Dependency> dependencies = configuration.getDependencies();

if(!dependencies.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Text;
import org.simpleframework.xml.Transient;
import org.simpleframework.xml.core.Commit;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.util.Dictionary;
Expand All @@ -34,9 +35,6 @@
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.repository.RemoteRepository;

import com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ClassInfo;

public class ConfigurationReader {

private final AtomicReference<WorkspaceConfiguration> workspaceReference;
Expand Down Expand Up @@ -137,44 +135,36 @@ private static class ProjectDefinition implements ProjectConfiguration {
private List<DependencyDefinition> dependencies;

@ElementList(entry="variable", required=false)
private Dictionary<VariableDefinition> environment;
private Dictionary<VariableDefinition> properties;

@Transient
private Map<String, Object> attributes;

@ElementList(entry="path", required=false)
private List<String> source;

private Set<ClassInfo> projectClasses;
private long lastModified;

public ProjectDefinition() {
this.lastModified = System.currentTimeMillis();
this.dependencies = new ArrayList<DependencyDefinition>();
this.environment = new Dictionary<VariableDefinition>();
this.properties = new Dictionary<VariableDefinition>();
this.attributes = new ConcurrentHashMap<String, Object>();
this.source = new ArrayList<String>();
}

public Map<String, String> getEnvironmentVariables() {
@Override
public Map<String, String> getProperties() {
Map<String, String> map = new LinkedHashMap<String, String>();

if(environment != null) {
for(VariableDefinition data : environment) {
if(properties != null) {
for(VariableDefinition data : properties) {
map.put(data.name, data.value);
}
}
return map;
}

@Override
public Set<ClassInfo> getAllClasses() {
if(projectClasses == null) {
try {
projectClasses = ClassPath.from(Thread.currentThread().getContextClassLoader()).getAllClasses();
}catch(Exception e) {
projectClasses = Collections.emptySet();
}
}
return projectClasses;
}

@Override
public List<Dependency> getDependencies() {
return Collections.<Dependency>unmodifiableList(dependencies);
Expand All @@ -189,6 +179,16 @@ public ProjectLayout getProjectLayout() {
public long getLastModifiedTime() {
return lastModified;
}

@Override
public <T> T getAttribute(String name) {
return (T)attributes.get(name);
}

@Override
public void setAttribute(String name, Object value) {
attributes.put(name, value);
}
}

@Root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.snapscript.studio.project.ProjectLayout;

import com.google.common.reflect.ClassPath.ClassInfo;

public interface ProjectConfiguration {

String PROJECT_FILE = ".project";
String CLASSPATH_FILE = ".classpath";

List<Dependency> getDependencies();
Map<String, String> getEnvironmentVariables();
Set<ClassInfo> getAllClasses();
Map<String, String> getProperties();
<T> T getAttribute(String name);
void setAttribute(String name, Object value);
ProjectLayout getProjectLayout();
long getLastModifiedTime();
}
2 changes: 1 addition & 1 deletion snap-studio/work/demo/games/src/mario/MarioGame.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ function start(scale) {
}
}

start(2);
start(2);

0 comments on commit ca6148e

Please sign in to comment.