/* Copyright (C) 2009 Mobile Sorcery AB
This program is free software; you can redistribute it and/or modify it
under the terms of the Eclipse Public License v1.0.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the Eclipse Public License v1.0 for
more details.
You should have received a copy of the Eclipse Public License v1.0 along
with this program. It is also available at http://www.eclipse.org/legal/epl-v10.html
*/
package com.mobilesorcery.sdk.internal.dependencies;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import com.mobilesorcery.sdk.core.CoreMoSyncPlugin;
import com.mobilesorcery.sdk.core.IBuildVariant;
import com.mobilesorcery.sdk.core.MoSyncBuilder;
import com.mobilesorcery.sdk.core.MoSyncProject;
import com.mobilesorcery.sdk.internal.PipeTool;
import com.mobilesorcery.sdk.internal.builder.MoSyncResourceBuilderVisitor;
public class ResourceFileDependencyProvider implements
IDependencyProvider<IResource> {
private static final Map<IResource, Collection<IResource>> EMPTY = new HashMap<IResource, Collection<IResource>>();
private final MoSyncProject project;
private final IBuildVariant variant;
public ResourceFileDependencyProvider(MoSyncProject project, IBuildVariant variant) {
this.project = project;
this.variant = variant;
}
@Override
public Map<IResource, Collection<IResource>> computeDependenciesOf(IResource obj) throws CoreException {
// TODO: Use centralized:
IPath resourceOutputPath = MoSyncBuilder.getResourceOutputPath(project.getWrappedProject(), variant);
IFile[] resourceOutputFiles = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(resourceOutputPath.toFile().toURI());
// We use a mini-hack to get the desired results;
// first, we let every .lst file depend on the project's
// resource file. The dependencies generated by pipetool
// is an aggregate of all resources in the project's .lst files.
HashMap<IResource, Collection<IResource>> result = new HashMap<IResource, Collection<IResource>>();
for (IFile resourceOutputFile : resourceOutputFiles) {
if (MoSyncBuilder.isResourceFile(obj)) {
result.put(obj, new ArrayList(Arrays.asList(resourceOutputFile)));
} else if (obj.equals(resourceOutputFile)) {
try {
GCCDependencyFileParser parser = new GCCDependencyFileParser();
IPath depsFile = PipeTool.getResourcesDependencyFile(project.getWrappedProject(), variant);
parser.parse(depsFile.toFile());
result.putAll(parser.getDependencies());
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR,
CoreMoSyncPlugin.PLUGIN_ID, e.getMessage(), e));
}
}
}
return result;
}
}