<commit_before>;*******************************************************************************
;* Copyright (c) 2010 <NAME>.
;* All rights reserved. This program and the accompanying materials
;* are made available under the terms of the Eclipse Public License v1.0
;* which accompanies this distribution, and is available at
;* http://www.eclipse.org/legal/epl-v10.html
;*
;* Contributors: 
;*    <NAME> - initial API and implementation
;*******************************************************************************/
(ns ccw.support.labrepl.wizards.LabreplCreationOperation
  (:import
     [java.lang.reflect InvocationTargetException]
     [java.net URL]
     [java.util.zip ZipFile]
     [org.eclipse.core.runtime NullProgressMonitor
                            SubProgressMonitor
                            CoreException
                            IStatus
                            Platform
                            Status
                            FileLocator]
     [org.eclipse.ui.dialogs IOverwriteQuery]
     [org.eclipse.jdt.core JavaCore]
     [org.eclipse.debug.core ILaunchManager]
     [java.io IOException]
     [org.eclipse.core.resources ResourcesPlugin]
     [org.eclipse.jface.viewers StructuredSelection]
     [ccw ClojureCore]
     [ccw.support.labrepl Activator]
     [ccw.launching ClojureLaunchShortcut]
     [org.eclipse.ui.wizards.datatransfer ImportOperation
                                          ZipFileStructureProvider])
  (:use
    [leiningen.core :only [read-project defproject]]
    [leiningen.deps :only [deps]])
  (:gen-class
   :implements [org.eclipse.jface.operation.IRunnableWithProgress]
   :constructors {[ccw.support.labrepl.wizards.LabreplCreateProjectPage org.eclipse.ui.dialogs.IOverwriteQuery] []}
   :init myinit
   :state state))

(defn- -myinit
  [pages overwrite-query]
  [[] (ref {:page pages :overwrite-query overwrite-query})])

(defn config-new-project
  [root name monitor]
  (try
    (let
      [project (.getProject root name)]
      (if (not (.exists project)) (.create project nil))
      (if (not (.isOpen project)) (.open project nil))
      (let
        [desc (.getDescription project)]
        (.setLocation desc nil)
        (.setDescription project desc (SubProgressMonitor. monitor 1))
        project))
    (catch CoreException exception (throw (InvocationTargetException. exception)))))

(defn get-zipfile-from-plugin-dir
  [plugin-relative-path]
  (try
    (let
      [bundle (.getBundle (Activator/getDefault))
        starter-url (URL. (.getEntry bundle "/") plugin-relative-path)]
      (ZipFile. (.getFile (FileLocator/toFileURL starter-url))))
    (catch IOException exception
      (let
        [message (str plugin-relative-path ": " (.getMessage exception))
          status (Status. IStatus/ERROR Activator/PLUGIN_ID IStatus/ERROR  message exception)]
        (throw (CoreException. status))))))

(defn import-files-from-zip
  [src-zip-file dest-path monitor overwrite-query]
  (let
    [structure-provider (ZipFileStructureProvider. src-zip-file)
      op (ImportOperation. dest-path (.getRoot structure-provider) structure-provider overwrite-query)]
    (.run op monitor)))

(defn make-project-folder
  [project folder monitor]
  (let [project-folder (.getFolder project folder)]
    (.create project-folder true true monitor)))

(defn do-imports
  [project monitor overwrite-query]
  (try
    (let
      [dest-path (.getFullPath project)
       ; additional-folders ["lib" "classes" "bin"] TODO re-add lib
       additional-folders ["classes" "bin"]
        zip-file (get-zipfile-from-plugin-dir "examples/labrepl.zip")]
      (doall (map #(make-project-folder project % monitor) additional-folders))
      (import-files-from-zip zip-file dest-path (SubProgressMonitor. monitor 1) overwrite-query))
    (catch CoreException exception (throw (InvocationTargetException. exception)))))

(defn fix-libraries
  "Enter all the JAR files in the lib directory to the Java build path of the project"
  [project]
  (let
    [java-project (.getJavaProject (ClojureCore/getClojureProject project))
     old-lib-entries (vec (.getRawClasspath java-project))
     lib-members (.members (.getFolder project "lib"))
     new-lib-entries
       (into-array 
         (concat old-lib-entries (map #(JavaCore/newLibraryEntry (.getFullPath %) nil nil) lib-members)))]
    (doto java-project
      (.setRawClasspath new-lib-entries nil)
      (.save nil true))))

(defn create-project
  [root page monitor overwrite-query]
  
  (.beginTask monitor "Configuring project..." 1)
  
  (let
    [project-name (.getProjectName page)
      project (config-new-project root project-name monitor)
      page-state @(.state page)
      run-lein-deps (.getSelection (:run-lein-deps-button page-state))]
    
    (do-imports project (SubProgressMonitor. monitor 1) overwrite-query)
    
    ; TODO handle run-repl
    (if run-lein-deps
      (let
        [leiningen-pfile (.toOSString (.getLocation (.getFile project "project.clj")))
          labrepl-leiningen-project (read-project (str leiningen-pfile))
          run-repl (.getSelection (:run-repl-button page-state))]
        #_(deps labrepl-leiningen-project) ; TODO enable
        (println "lein deps is disabled")
        (fix-libraries project)
        (if run-repl
          (let [startup-file-selection (StructuredSelection. (.getFile (.getFolder project "src") "labrepl.clj"))]
            (.launch (ClojureLaunchShortcut.) startup-file-selection ILaunchManager/RUN_MODE)))))))<commit_msg>Re-enable "lein deps", and first attempt to fire up the main program of labrepl.clj, but it's not working yet.<commit_after>;*******************************************************************************
;* Copyright (c) 2010 <NAME>.
;* All rights reserved. This program and the accompanying materials
;* are made available under the terms of the Eclipse Public License v1.0
;* which accompanies this distribution, and is available at
;* http://www.eclipse.org/legal/epl-v10.html
;*
;* Contributors: 
;*    <NAME> - initial API and implementation
;*******************************************************************************/
(ns ccw.support.labrepl.wizards.LabreplCreationOperation
  (:import
     [java.lang.reflect InvocationTargetException]
     [java.net URL]
     [java.util.zip ZipFile]
     [org.eclipse.core.runtime NullProgressMonitor
                            SubProgressMonitor
                            CoreException
                            IStatus
                            Platform
                            Status
                            FileLocator]
     [org.eclipse.ui.dialogs IOverwriteQuery]
     [org.eclipse.jdt.core JavaCore]
     [org.eclipse.debug.core ILaunchManager]
     [java.io IOException]
     [org.eclipse.core.resources IResource ResourcesPlugin]
     [org.eclipse.jface.viewers StructuredSelection]
     [ccw ClojureCore]
     [ccw.support.labrepl Activator]
     [ccw.launching ClojureLaunchShortcut]
     [ccw.editors.antlrbased EvaluateTextAction]
     [org.eclipse.ui.wizards.datatransfer ImportOperation
                                          ZipFileStructureProvider])
  (:use
    [leiningen.core :only [read-project defproject]]
    [leiningen.deps :only [deps]])
  (:gen-class
   :implements [org.eclipse.jface.operation.IRunnableWithProgress]
   :constructors {[ccw.support.labrepl.wizards.LabreplCreateProjectPage org.eclipse.ui.dialogs.IOverwriteQuery] []}
   :init myinit
   :state state))

(defn- -myinit
  [pages overwrite-query]
  [[] (ref {:page pages :overwrite-query overwrite-query})])

(defn config-new-project
  [root name monitor]
  (try
    (let
      [project (.getProject root name)]
      (if (not (.exists project)) (.create project nil))
      (if (not (.isOpen project)) (.open project nil))
      (let
        [desc (.getDescription project)]
        (.setLocation desc nil)
        (.setDescription project desc (SubProgressMonitor. monitor 1))
        project))
    (catch CoreException exception (throw (InvocationTargetException. exception)))))

(defn get-zipfile-from-plugin-dir
  [plugin-relative-path]
  (try
    (let
      [bundle (.getBundle (Activator/getDefault))
        starter-url (URL. (.getEntry bundle "/") plugin-relative-path)]
      (ZipFile. (.getFile (FileLocator/toFileURL starter-url))))
    (catch IOException exception
      (let
        [message (str plugin-relative-path ": " (.getMessage exception))
          status (Status. IStatus/ERROR Activator/PLUGIN_ID IStatus/ERROR  message exception)]
        (throw (CoreException. status))))))

(defn import-files-from-zip
  [src-zip-file dest-path monitor overwrite-query]
  (let
    [structure-provider (ZipFileStructureProvider. src-zip-file)
      op (ImportOperation. dest-path (.getRoot structure-provider) structure-provider overwrite-query)]
    (.run op monitor)))

(defn make-project-folder
  [project folder monitor]
  (let [project-folder (.getFolder project folder)]
    (.create project-folder true true monitor)))

(defn do-imports
  [project monitor overwrite-query]
  (try
    (let
      [dest-path (.getFullPath project)
        additional-folders ["lib" "classes" "bin"]
        zip-file (get-zipfile-from-plugin-dir "examples/labrepl.zip")]
      (doall (map #(make-project-folder project % monitor) additional-folders))
      (import-files-from-zip zip-file dest-path (SubProgressMonitor. monitor 1) overwrite-query))
    (catch CoreException exception (throw (InvocationTargetException. exception)))))

(defn fix-libraries
  "Enter all the JAR files in the lib directory to the Java build path of the project"
  [project]
  (let
    [java-project (.getJavaProject (ClojureCore/getClojureProject project))
     lib-folder (.getFolder project "lib")
     _ (.refreshLocal lib-folder (IResource/DEPTH_ONE) nil)
     lib-members (.members lib-folder)
     old-lib-entries (vec (.getRawClasspath java-project))
     new-lib-entries
       (into-array 
         (concat old-lib-entries (map #(JavaCore/newLibraryEntry (.getFullPath %) nil nil) lib-members)))]
    (doto java-project
      (.setRawClasspath new-lib-entries nil)
      (.save nil true))))

(defn create-project
  [root page monitor overwrite-query]
  
  (.beginTask monitor "Configuring project..." 1)
  
  (let
    [project-name (.getProjectName page)
      project (config-new-project root project-name monitor)
      page-state @(.state page)
      run-lein-deps (.getSelection (:run-lein-deps-button page-state))]
    
    (do-imports project (SubProgressMonitor. monitor 1) overwrite-query)
    
    (if run-lein-deps
      (let
        [leiningen-pfile (.toOSString (.getLocation (.getFile project "project.clj")))
          labrepl-leiningen-project (read-project (str leiningen-pfile))
          run-repl (.getSelection (:run-repl-button page-state))]
        (deps labrepl-leiningen-project)
        (fix-libraries project)
        (if run-repl
          (let
            [startup-file-selection (StructuredSelection. (.getFile (.getFolder project "src") "labrepl.clj"))
              run-labrepl-action
                (proxy [EvaluateTextAction] ["Start labrepl"]
                  (run [] (.evaluateText "(labrepl/-main)")))]
            (.launch (ClojureLaunchShortcut.) startup-file-selection ILaunchManager/RUN_MODE)
            ; TODO the following does not work
            #_(.run run-labrepl-action)))))))
{'fraction_non_alphanumeric': 0.14179038917684267, 'fraction_numerical': 0.002108407274005095, 'mean_word_length': 2.939100346020761, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2773185', 'n_tokens_mistral': 3427, 'n_tokens_neox': 3241, 'n_words': 814}