overleaf/services/web/public/coffee/ide/file-tree/FileTreeManager.coffee
James Allen 662122bb1c CI and local dev environment improvements
The need for this became very noticeable due to the slowness of filesystem access in docker-in-mac, with a full compile taking over a minute for me in docker. Using make to introduce incremental compile makes this near instantaneous outside of docker (if only a few files have changed), and quick enough inside docker.

With incremental compile via make, it compiles quickly enough that re-compiling and restarting the web service automatically when backend files change is quick enough now. This is how the service is run via docker-compose in https://github.com/sharelatex/sharelatex-dev-environment, so it shouldn't be necessary to manually restart the container each time a coffee file changes.

At the moment Jenkins pull web modules in via the GitSCM plugin, but I believe this is creating a dependency in Jenkins, where any commits to any of the modules causes all of the web branches to rebuild. By doing it via our own scripts we can hopefully avoid this. It also creates a build process which is reproducible locally.

**Note that at the moment in this PR all modules pull from `ja-dockerize-dev` branches, but these should be merged first, and this PR updated to point to the master branches before merging**. This is necessary for other changes to build process/docker-compose workflow.

As well as a Makefile for web, there is now a `Makefile.module`. This is copied into each module directory by the top-level Makefile, and is written in a way to be flexible and support unit tests, acceptance tests, front-end js for the ide and main, and the modules `app/coffee` directory, while allowing modules to have some of these missing (not all modules have e.g. acceptance tests, or front-end JS). This will allows us to refine the build process in future, without needing to update the Makefile in each module repo separately (I found this to be a painful part of this development).

This makes web compatible with the docker-compose workflow at https://github.com/sharelatex/sharelatex-dev-environment, where each service is running in its own docker container, with networking managed by docker.

Previously the Makefile was set up to run unit tests in docker with `make unit_tests`. This now just runs them natively. In the CI, they are run in docker anyway (all steps in Jenkins are), and locally, they run fine natively with `npm run test:unit`, or can be run in docker via https://github.com/sharelatex/sharelatex-dev-environment with `bin/run web_sl npm run test:unit`.

Previously we did a lot of juggling with only mounting source files (coffee, less, etc) into the docker container for acceptance tests. This was to avoid creating root owned files if the whole directory was mounted. Now instead the whole web directory is mounted read-only, with the compilation step done outside of the container before running the tests.

This allows the host and container to share the `node_modules` folder as well, which avoids needing to `npm install` twice on the CI box, and should speed up the build by a few minutes.

On macOS, this would cause a problem with compiled modules if you tried to use the same `node_modules` to run the app natively. However, if running via docker-compose in https://github.com/sharelatex/sharelatex-dev-environment, this is no longer a problem.
2018-01-15 09:36:41 +00:00

396 lines
11 KiB
CoffeeScript

define [
"ide/file-tree/directives/fileEntity"
"ide/file-tree/directives/draggable"
"ide/file-tree/directives/droppable"
"ide/file-tree/controllers/FileTreeController"
"ide/file-tree/controllers/FileTreeEntityController"
"ide/file-tree/controllers/FileTreeFolderController"
"ide/file-tree/controllers/FileTreeRootFolderController"
], () ->
class FileTreeManager
constructor: (@ide, @$scope) ->
@$scope.$on "project:joined", =>
@loadRootFolder()
@loadDeletedDocs()
@$scope.$emit "file-tree:initialized"
@$scope.$watch "rootFolder", (rootFolder) =>
if rootFolder?
@recalculateDocList()
@_bindToSocketEvents()
@$scope.multiSelectedCount = 0
$(document).on "click", =>
@clearMultiSelectedEntities()
@$scope.$digest()
_bindToSocketEvents: () ->
@ide.socket.on "reciveNewDoc", (parent_folder_id, doc) =>
parent_folder = @findEntityById(parent_folder_id) or @$scope.rootFolder
@$scope.$apply () =>
parent_folder.children.push {
name: doc.name
id: doc._id
type: "doc"
}
@recalculateDocList()
@ide.socket.on "reciveNewFile", (parent_folder_id, file) =>
parent_folder = @findEntityById(parent_folder_id) or @$scope.rootFolder
@$scope.$apply () =>
parent_folder.children.push {
name: file.name
id: file._id
type: "file"
}
@recalculateDocList()
@ide.socket.on "reciveNewFolder", (parent_folder_id, folder) =>
parent_folder = @findEntityById(parent_folder_id) or @$scope.rootFolder
@$scope.$apply () =>
parent_folder.children.push {
name: folder.name
id: folder._id
type: "folder"
children: []
}
@recalculateDocList()
@ide.socket.on "reciveEntityRename", (entity_id, name) =>
entity = @findEntityById(entity_id)
return if !entity?
@$scope.$apply () =>
entity.name = name
@recalculateDocList()
@ide.socket.on "removeEntity", (entity_id) =>
entity = @findEntityById(entity_id)
return if !entity?
@$scope.$apply () =>
@_deleteEntityFromScope entity
@recalculateDocList()
@$scope.$broadcast "entity:deleted", entity
@ide.socket.on "reciveEntityMove", (entity_id, folder_id) =>
entity = @findEntityById(entity_id)
folder = @findEntityById(folder_id)
@$scope.$apply () =>
@_moveEntityInScope(entity, folder)
@recalculateDocList()
selectEntity: (entity) ->
@selected_entity_id = entity.id # For reselecting after a reconnect
@ide.fileTreeManager.forEachEntity (entity) ->
entity.selected = false
entity.selected = true
toggleMultiSelectEntity: (entity) ->
entity.multiSelected = !entity.multiSelected
@$scope.multiSelectedCount = @multiSelectedCount()
multiSelectedCount: () ->
count = 0
@forEachEntity (entity) ->
if entity.multiSelected
count++
return count
getMultiSelectedEntities: () ->
entities = []
@forEachEntity (e) ->
if e.multiSelected
entities.push e
return entities
getMultiSelectedEntityChildNodes: () ->
entities = @getMultiSelectedEntities()
paths = {}
for entity in entities
paths[@getEntityPath(entity)] = entity
prefixes = {}
for path, entity of paths
parts = path.split("/")
if parts.length <= 1
continue
else
# Record prefixes a/b/c.tex -> 'a' and 'a/b'
for i in [1..(parts.length - 1)]
prefixes[parts.slice(0,i).join("/")] = true
child_entities = []
for path, entity of paths
# If the path is in the prefixes, then it's a parent folder and
# should be ignore
if !prefixes[path]?
child_entities.push entity
return child_entities
clearMultiSelectedEntities: () ->
return if @$scope.multiSelectedCount == 0 # Be efficient, this is called a lot on 'click'
@forEachEntity (entity) ->
entity.multiSelected = false
@$scope.multiSelectedCount = 0
multiSelectSelectedEntity: () ->
@findSelectedEntity()?.multiSelected = true
existsInFolder: (folder_id, name) ->
folder = @findEntityById(folder_id)
return false if !folder?
entity = @_findEntityByPathInFolder(folder, name)
return entity?
findSelectedEntity: () ->
selected = null
@forEachEntity (entity) ->
selected = entity if entity.selected
return selected
findEntityById: (id, options = {}) ->
return @$scope.rootFolder if @$scope.rootFolder.id == id
entity = @_findEntityByIdInFolder @$scope.rootFolder, id
return entity if entity?
if options.includeDeleted
for entity in @$scope.deletedDocs
return entity if entity.id == id
return null
_findEntityByIdInFolder: (folder, id) ->
for entity in folder.children or []
if entity.id == id
return entity
else if entity.children?
result = @_findEntityByIdInFolder(entity, id)
return result if result?
return null
findEntityByPath: (path) ->
@_findEntityByPathInFolder @$scope.rootFolder, path
_findEntityByPathInFolder: (folder, path) ->
if !path? or !folder?
return null
parts = path.split("/")
name = parts.shift()
rest = parts.join("/")
if name == "."
return @_findEntityByPathInFolder(folder, rest)
for entity in folder.children
if entity.name == name
if rest == ""
return entity
else if entity.type == "folder"
return @_findEntityByPathInFolder(entity, rest)
return null
forEachEntity: (callback = (entity, parent_folder, path) ->) ->
@_forEachEntityInFolder(@$scope.rootFolder, null, callback)
for entity in @$scope.deletedDocs or []
callback(entity)
_forEachEntityInFolder: (folder, path, callback) ->
for entity in folder.children or []
if path?
childPath = path + "/" + entity.name
else
childPath = entity.name
callback(entity, folder, childPath)
if entity.children?
@_forEachEntityInFolder(entity, childPath, callback)
getEntityPath: (entity) ->
@_getEntityPathInFolder @$scope.rootFolder, entity
_getEntityPathInFolder: (folder, entity) ->
for child in folder.children or []
if child == entity
return entity.name
else if child.type == "folder"
path = @_getEntityPathInFolder(child, entity)
if path?
return child.name + "/" + path
return null
getRootDocDirname: () ->
rootDoc = @findEntityById @$scope.project.rootDoc_id
return if !rootDoc?
path = @getEntityPath(rootDoc)
return if !path?
return path.split("/").slice(0, -1).join("/")
loadRootFolder: () ->
@$scope.rootFolder = @_parseFolder(@$scope?.project?.rootFolder[0])
_parseFolder: (rawFolder) ->
folder = {
name: rawFolder.name
id: rawFolder._id
type: "folder"
children: []
selected: (rawFolder._id == @selected_entity_id)
}
for doc in rawFolder.docs or []
folder.children.push {
name: doc.name
id: doc._id
type: "doc"
selected: (doc._id == @selected_entity_id)
}
for file in rawFolder.fileRefs or []
folder.children.push {
name: file.name
id: file._id
type: "file"
selected: (file._id == @selected_entity_id)
}
for childFolder in rawFolder.folders or []
folder.children.push @_parseFolder(childFolder)
return folder
loadDeletedDocs: () ->
@$scope.deletedDocs = []
for doc in @$scope.project.deletedDocs or []
@$scope.deletedDocs.push {
name: doc.name
id: doc._id
type: "doc"
deleted: true
}
recalculateDocList: () ->
@$scope.docs = []
@forEachEntity (entity, parentFolder, path) =>
if entity.type == "doc" and !entity.deleted
@$scope.docs.push {
doc: entity
path: path
}
# Keep list ordered by folders, then name
@$scope.docs.sort (a,b) ->
aDepth = (a.path.match(/\//g) || []).length
bDepth = (b.path.match(/\//g) || []).length
if aDepth - bDepth != 0
return -(aDepth - bDepth) # Deeper path == folder first
else if a.path < b.path
return -1
else
return 1
getEntityPath: (entity) ->
@_getEntityPathInFolder @$scope.rootFolder, entity
_getEntityPathInFolder: (folder, entity) ->
for child in folder.children or []
if child == entity
return entity.name
else if child.type == "folder"
path = @_getEntityPathInFolder(child, entity)
if path?
return child.name + "/" + path
return null
getCurrentFolder: () ->
# Return the root folder if nothing is selected
@_getCurrentFolder(@$scope.rootFolder) or @$scope.rootFolder
_getCurrentFolder: (startFolder = @$scope.rootFolder) ->
for entity in startFolder.children or []
# The 'current' folder is either the one selected, or
# the one containing the selected doc/file
if entity.selected
if entity.type == "folder"
return entity
else
return startFolder
if entity.type == "folder"
result = @_getCurrentFolder(entity)
return result if result?
return null
createDoc: (name, parent_folder = @getCurrentFolder()) ->
# We'll wait for the socket.io notification to actually
# add the doc for us.
@ide.$http.post "/project/#{@ide.project_id}/doc", {
name: name,
parent_folder_id: parent_folder?.id
_csrf: window.csrfToken
}
createFolder: (name, parent_folder = @getCurrentFolder()) ->
# We'll wait for the socket.io notification to actually
# add the folder for us.
return @ide.$http.post "/project/#{@ide.project_id}/folder", {
name: name,
parent_folder_id: parent_folder?.id
_csrf: window.csrfToken
}
renameEntity: (entity, name, callback = (error) ->) ->
return if entity.name == name
if name.length < 150
entity.name = name
return @ide.$http.post "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}/rename", {
name: entity.name,
_csrf: window.csrfToken
}
deleteEntity: (entity, callback = (error) ->) ->
# We'll wait for the socket.io notification to
# delete from scope.
return @ide.queuedHttp {
method: "DELETE"
url: "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}"
headers:
"X-Csrf-Token": window.csrfToken
}
moveEntity: (entity, parent_folder, callback = (error) ->) ->
# Abort move if the folder being moved (entity) has the parent_folder as child
# since that would break the tree structure.
return if @_isChildFolder(entity, parent_folder)
@_moveEntityInScope(entity, parent_folder)
return @ide.queuedHttp.post "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}/move", {
folder_id: parent_folder.id
_csrf: window.csrfToken
}
_isChildFolder: (parent_folder, child_folder) ->
parent_path = @getEntityPath(parent_folder) or "" # null if root folder
child_path = @getEntityPath(child_folder) or "" # null if root folder
# is parent path the beginning of child path?
return (child_path.slice(0, parent_path.length) == parent_path)
_deleteEntityFromScope: (entity, options = { moveToDeleted: true }) ->
return if !entity?
parent_folder = null
@forEachEntity (possible_entity, folder) ->
if possible_entity == entity
parent_folder = folder
if parent_folder?
index = parent_folder.children.indexOf(entity)
if index > -1
parent_folder.children.splice(index, 1)
if entity.type == "doc" and options.moveToDeleted
entity.deleted = true
@$scope.deletedDocs.push entity
_moveEntityInScope: (entity, parent_folder) ->
return if entity in parent_folder.children
@_deleteEntityFromScope(entity, moveToDeleted: false)
parent_folder.children.push(entity)