Skip to content

Commit

Permalink
[ 1922451 ] [Wizard] generate .cabal file for new Haskell project in ws
Browse files Browse the repository at this point in the history
darcs-hash:20080321212358-34f1c-dab828e326ea252b40d1071043f387af94941cab.gz
  • Loading branch information
Leif Frenzel committed Mar 21, 2008
1 parent c9d0f98 commit 3708c5d
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package net.sf.eclipsefp.haskell.core.project;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -22,91 +21,95 @@ public class HaskellProjectCreationOperation_PDETest extends

@Override
protected ProjectCreationOperation createOperation() {
fStore = new Preferences();
fStore.setValue(ICorePreferenceNames.FOLDERS_IN_NEW_PROJECT, true);
return new HaskellProjectCreationOperation(fStore);
}
fStore = new Preferences();
fStore.setValue( ICorePreferenceNames.FOLDERS_IN_NEW_PROJECT, true );
return new HaskellProjectCreationOperation( fStore );
}

public void testAddsHaskellNature() throws InvocationTargetException,
InterruptedException, CoreException {
runOperation();
public void testAddsHaskellNature() throws InvocationTargetException,
InterruptedException, CoreException {
runOperation();

IProject prj = getWorkspaceRoot().getProject(PROJECT_NAME);
assertNotNull(prj.getNature(HaskellNature.NATURE_ID));
}
IProject prj = getWorkspaceRoot().getProject( PROJECT_NAME );
assertNotNull( prj.getNature( HaskellNature.NATURE_ID ) );
}

public void testCreatesDirectoriesFromPreferences()
throws InvocationTargetException, InterruptedException {
fStore.setValue(ICorePreferenceNames.FOLDERS_SRC, "customSrc");
fStore.setValue(ICorePreferenceNames.FOLDERS_OUT, "customOut");
fStore.setValue(ICorePreferenceNames.FOLDERS_BIN, "customBin");
throws InvocationTargetException, InterruptedException {
fStore.setValue( ICorePreferenceNames.FOLDERS_SRC, "customSrc" );
fStore.setValue( ICorePreferenceNames.FOLDERS_OUT, "customOut" );
fStore.setValue( ICorePreferenceNames.FOLDERS_BIN, "customBin" );

runOperation();
IProject prj = getWorkspaceRoot().getProject(PROJECT_NAME);
runOperation();
IProject prj = getWorkspaceRoot().getProject( PROJECT_NAME );

assertValid(prj.getFolder("customSrc"));
assertValid(prj.getFolder("customOut"));
assertValid(prj.getFolder("customBin"));
}
assertValid( prj.getFolder( "customSrc" ) );
assertValid( prj.getFolder( "customOut" ) );
assertValid( prj.getFolder( "customBin" ) );
}

public void testCreatesDescriptorFile() throws InvocationTargetException,
InterruptedException {
runOperation();
IProject prj = getWorkspaceRoot().getProject(PROJECT_NAME);
IFile f = prj.getFile(HaskellProjectManager.HASKELL_PROJECT_DESCRIPTOR);
assertValid(f);
}

public void testSetsUpProjectFoldersFromPreferences()
throws InvocationTargetException, InterruptedException,
CoreException, IOException {
fStore.setValue(ICorePreferenceNames.FOLDERS_SRC, "mySrc");
fStore.setValue(ICorePreferenceNames.FOLDERS_OUT, "myOut");
fStore.setValue(ICorePreferenceNames.FOLDERS_BIN, "myBin");
fStore.setValue(ICorePreferenceNames.TARGET_BINARY, "myBinary");
fStore.setValue(ICorePreferenceNames.SELECTED_COMPILER, "null");

runOperation();
IProject prj = getWorkspaceRoot().getProject(PROJECT_NAME);
IFile f = prj.getFile(HaskellProjectManager.HASKELL_PROJECT_DESCRIPTOR);
final String expectedContents = HaskellProjectManager
.createDescriptorContent("mySrc", "myOut", "myBin", "myBinary", "null");
assertEquals(expectedContents, readContents(f));

}

public void testDoNotCreateFoldersWhenPreferenceDisabled()
throws InvocationTargetException, InterruptedException,
CoreException, IOException {
fStore.setValue(ICorePreferenceNames.FOLDERS_IN_NEW_PROJECT, false);

runOperation();

IProject prj = getWorkspaceRoot().getProject(PROJECT_NAME);
//should only contain the project descriptors (.project and .hsproject)
assertEquals(2, prj.members().length);

IFile f = prj.getFile(HaskellProjectManager.HASKELL_PROJECT_DESCRIPTOR);
assertValid(f);
assertEquals("", readContents(f));
}

private String readContents(final IFile file) throws CoreException, IOException {
StringBuffer buf = new StringBuffer(1024);
InputStream input = file.getContents();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
while (null != (line = reader.readLine())) {
buf.append(line);
buf.append('\n');
}
input.close();
return buf.toString();
}

private void assertValid(final IResource res) {
assertNotNull(res);
assertTrue("Resource does not exist", res.exists());
}
InterruptedException {
runOperation();
IProject prj = getWorkspaceRoot().getProject( PROJECT_NAME );
IFile f = prj.getFile( HaskellProjectManager.HASKELL_PROJECT_DESCRIPTOR );
assertValid( f );

assertValid( prj.getFile( prj.getName() + ".cabal" ) );
assertValid( prj.getFile( "Setup.hs" ) );
}

public void testSetsUpProjectFoldersFromPreferences() throws Exception {
fStore.setValue( ICorePreferenceNames.FOLDERS_SRC, "mySrc" );
fStore.setValue( ICorePreferenceNames.FOLDERS_OUT, "myOut" );
fStore.setValue( ICorePreferenceNames.FOLDERS_BIN, "myBin" );
fStore.setValue( ICorePreferenceNames.TARGET_BINARY, "myBinary" );
fStore.setValue( ICorePreferenceNames.SELECTED_COMPILER, "null" );

runOperation();
IProject prj = getWorkspaceRoot().getProject( PROJECT_NAME );
IFile f = prj.getFile( HaskellProjectManager.HASKELL_PROJECT_DESCRIPTOR );
final String expectedContents = HaskellProjectManager
.createDescriptorContent( "mySrc", "myOut", "myBin", "myBinary", "null" );
assertEquals( expectedContents, readContents( f ) );

}

public void testDoNotCreateFoldersWhenPreferenceDisabled() throws Exception {
fStore.setValue( ICorePreferenceNames.FOLDERS_IN_NEW_PROJECT, false );

runOperation();

IProject prj = getWorkspaceRoot().getProject( PROJECT_NAME );
// should only contain the project descriptors (.project and .hsproject)
// and the generated Setup.hs and cabal file
assertEquals( 4, prj.members().length );

IFile f = prj.getFile( HaskellProjectManager.HASKELL_PROJECT_DESCRIPTOR );
assertValid( f );
assertEquals( "", readContents( f ) );
}


// helping methods
//////////////////

private String readContents( final IFile file ) throws Exception {
StringBuffer buf = new StringBuffer( 1024 );
InputStream input = file.getContents();
BufferedReader reader = new BufferedReader( new InputStreamReader( input ) );
String line;
while( null != ( line = reader.readLine() ) ) {
buf.append( line );
buf.append( '\n' );
}
input.close();
return buf.toString();
}

private void assertValid( final IResource res ) {
assertNotNull( res );
assertTrue( "Resource does not exist", res.exists() );
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
// Copyright (c) 2003-2005 by Leif Frenzel - see http://leiffrenzel.de
package net.sf.eclipsefp.haskell.core.builder;

import org.eclipse.core.resources.*;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.runtime.IProgressMonitor;

/** <p>Visits the resource tree to build Haskell projects.</p>
*
*
* @author Leif Frenzel
*/
class BuildVisitor extends Visitor implements IResourceVisitor {

BuildVisitor( final IProgressMonitor monitor ) {
super( monitor );
}

public boolean visit( final IResource res ) {
//build the specified resource.
//return true to continue visiting children.
if( res instanceof IFile ) {
IFile file = ( IFile )res;
if( isHaskellFile( file ) ) {
if( isHaskellFile( file ) && isInSourceFolder( file ) ) {
compileFile( file );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package net.sf.eclipsefp.haskell.core.builder;

import java.util.Collection;
import java.util.Set;
import net.sf.eclipsefp.haskell.core.HaskellCorePlugin;
import net.sf.eclipsefp.haskell.core.compiler.ICompilerOutput;
import net.sf.eclipsefp.haskell.core.compiler.ICompilerOutputItem;
Expand All @@ -12,6 +13,7 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;


Expand All @@ -35,11 +37,25 @@ boolean isHaskellFile( final IFile file ) {
return ResourceUtil.hasHaskellExtension( file );
}

boolean isInSourceFolder( final IFile file ) {
if( file == null || !file.isAccessible() ) {
throw new IllegalArgumentException();
}
boolean result = false;
IHaskellProject hsProject = HaskellProjectManager.get( file.getProject() );
Set<IPath> sourcePaths = hsProject.getSourcePaths();
for( IPath sourcePath: sourcePaths ) {
IPath src = file.getProject().getFullPath().append( sourcePath );
result |= src.isPrefixOf( file.getFullPath() );
}
return result;
}

void compileFile( final IFile file ) {
IHaskellProject hsProject = HaskellProjectManager.get(file.getProject());
ICompilerOutput out = hsProject.compile(file);
IHaskellProject hsProject = HaskellProjectManager.get( file.getProject() );
ICompilerOutput out = hsProject.compile( file );
deleteMarkers( file );
markResource( file, out.getErrors());
markResource( file, out.getErrors() );
}

private void markResource( final IFile file,
Expand Down
Loading

0 comments on commit 3708c5d

Please sign in to comment.