2013-03-01 17 views
6

goolgeクロージャコンパイラで縮小されたjavascriptファイルを作成する保存アクションにフックするeclipseプラグインを作成しました。以下のファイルを参照してください。 それは日食まで働いた3.7.2。残念ながら、今では4.2.1では、これは時々無限ループを作り出すようです。ジョブ "compile .min.js"(ResourceChangedListener.javaの64行目)が原因であるようです。ワークスペースが何度も何度も繰り返し始める場合があります。これは、ジョブがビルドをトリガするファイルを作成または変更し、ビルドをトリガするジョブを再びトリガするためです。 しかし、私はこれを防ぐ方法を理解することはできません。juno(eclipse 4)への更新後にeclipseプラグインが動作しない

// Activator.java

package closure_compiler_save; 

import org.eclipse.core.resources.ResourcesPlugin; 
import org.eclipse.ui.plugin.AbstractUIPlugin; 
import org.osgi.framework.BundleContext; 

/** 
* The activator class controls the plug-in life cycle 
*/ 
public class Activator extends AbstractUIPlugin { 

    // The plug-in ID 
    public static final String PLUGIN_ID = "closure-compiler-save"; //$NON-NLS-1$ 

    // The shared instance 
    private static Activator plugin; 

    /** 
    * The constructor 
    */ 
    public Activator() { 
    } 

    @Override 
     public void start(BundleContext context) throws Exception { 
     super.start(context); 
     Activator.plugin = this; 

     ResourceChangedListener listener = new ResourceChangedListener(); 
      ResourcesPlugin.getWorkspace().addResourceChangeListener(listener); 
    } 

    @Override 
     public void stop(BundleContext context) throws Exception { 
     Activator.plugin = null; 
     super.stop(context); 
    } 

    /** 
    * Returns the shared instance 
    * 
    * @return the shared instance 
    */ 
    public static Activator getDefault() { 
     return plugin; 
    } 
} 

// ResourceChangedListener.java

package closure_compiler_save; 

import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import org.eclipse.core.resources.IFile; 
import org.eclipse.core.resources.IProject; 
import org.eclipse.core.resources.IResource; 
import org.eclipse.core.resources.IResourceChangeEvent; 
import org.eclipse.core.resources.IResourceChangeListener; 
import org.eclipse.core.resources.IResourceDelta; 
import org.eclipse.core.runtime.CoreException; 
import org.eclipse.core.runtime.IPath; 
import org.eclipse.core.runtime.IProgressMonitor; 
import org.eclipse.core.runtime.IStatus; 
import org.eclipse.core.runtime.Status; 
import org.eclipse.core.runtime.jobs.Job; 

public class ResourceChangedListener implements IResourceChangeListener { 

    public void resourceChanged(IResourceChangeEvent event) { 
     if (event.getType() != IResourceChangeEvent.POST_CHANGE) 
      return; 

     IResourceDelta delta = event.getDelta(); 
     try { 
      processDelta(delta); 
     } catch (CoreException e) { 
      e.printStackTrace(); 
     } 
    } 

    // find out which class files were just built 
    private void processDelta(IResourceDelta delta) throws CoreException { 

     IResourceDelta[] kids = delta.getAffectedChildren(); 
     for (IResourceDelta delta2 : kids) { 
      if (delta2.getAffectedChildren().length == 0) { 
       if (delta.getKind() != IResourceDelta.CHANGED) 
        return; 

       IResource res = delta2.getResource(); 
       if (res.getType() == IResource.FILE && "js".equalsIgnoreCase(res.getFileExtension())) { 
        if (res.getName().contains("min")) 
         return; 
        compile(res); 
       } 
      } 
      processDelta(delta2); 
     } 
    } 

    private void compile(final IResource res) throws CoreException { 

     final IPath fullPath = res.getFullPath(); 
     final IPath fullLocation = res.getLocation(); 
     final String fileName = fullPath.lastSegment().toString(); 
     final String outputFilename = fileName.substring(0, fileName.lastIndexOf(".")).concat(".min.js"); 
     final String outputPath = fullPath.removeFirstSegments(1).removeLastSegments(1).toString(); 

     final IProject project = res.getProject(); 
     final IFile newFile = project.getFile(outputPath.concat("/".concat(outputFilename))); 
     Job compileJob = new Job("Compile .min.js") { 
      public IStatus run(IProgressMonitor monitor) { 
       byte[] bytes = null; 
       try { 
        bytes = CallCompiler.compile(fullLocation.toString(), CallCompiler.SIMPLE_OPTIMIZATION).getBytes(); 

        InputStream source = new ByteArrayInputStream(bytes); 
        if (!newFile.exists()) { 
         newFile.create(source, IResource.NONE, null); 
        } else { 
         newFile.setContents(source, IResource.NONE, null); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } catch (CoreException e) { 
        e.printStackTrace(); 
       } 
       return Status.OK_STATUS; 
      } 
     }; 
     compileJob.setRule(newFile.getProject()); // this will ensure that no two jobs are writing simultaneously on the same file 
     compileJob.schedule(); 
    } 

} 

答えて

3

Iセットアップ空白日食Classic環境の後、そこに新しいEclipseプラグインプロジェクトを開始し、それが動作するすべてのファイルを再作成再び部分的に。 デバッグセッションを開始するこの環境では、.jsファイルを保存でき、.min.jsファイルは自動的に作成されます。 これまでのところとても良い! しかし、実際に開発しているEclipse環境にプラグインをインストールすると、自動保存は機能しません。

さらに少なくとも1ステップ!

ステップ2: マニフェストのようにビルドには含まれていないファイルがいくつかありました。彼らがなぜ非選択になったのか分かりません。 とにかく、空白のEclipse 4の古典を設定し、Eclipseのプラグインウィザードを使って、私の元の問題を修正したようです。私はまだ実際の問題が何だったのか知りたいです...

+0

CallCompilerはどこから来たのですか?それはあなたのEclipseプラグインフォルダに/ libディレクトリに入っているかもしれないサードパーティのjarからですか?実行した場合は、サードパーティ製のjarファイルがmanifest.mfエディタで編集されたManifest.mfランタイムパスにあることを確認し、bin.includesのbuild.propertiesにそのサードパーティ製のjarファイルが含まれるようにしてください。 – gamerson

+0

CallCompilerは私自身のクラスで、問題のファイルに対してクロージャーコンパイラを呼び出します。そのクラスのロジックは機能します。私はまた、同じCallCompilerクラスを持つファイルのminifyプロセスをトリガするポップアップメニューを持っています。 自動保存プロセスはもう機能しません。 – DarsVaeda

+0

Strangeのため、ワークスペース/ランタイムワークベンチでは動作しますが、スタンドアロンインストールでは動作しません。私は何度もそれを見てきました。コードを見ると、res.getLocation()呼び出しには注意が必要です。私はいくつかの状況でnullを返すことを見てきました。私の唯一の提案は、コンパイル()メソッドのデバッグ出力を追加して、devバージョンとインストールされたバージョンの違いを確認することです。 – gamerson

関連する問題