示例#1
0
        public void AsyncLock_ConcurrentLockFollowedByRelease()
        {
            ManualResetEvent done     = new ManualResetEvent(false);
            const int        MaxCount = 100;
            int count = 0;

            Parallel.For(0, MaxCount, async cnt =>
            {
                await _lockFile.LockAsync();

                // We are now within the lock
                await Task.Delay(5);
                count = count + 1;

                // Releasing lock
                _lockFile.Release();
                if (count == MaxCount)
                {
                    done.Set();
                }
            });

            done.WaitOne();
            Assert.Equal(MaxCount, count);
        }
示例#2
0
        /**
         * Uninstall a package asynchronously.
         */
        public async Task RemovePackageAsync(string userId, string projectName, string packageName)
        {
            var pwd = $"{GetBaseDir(userId, projectName)}";

            if (!Directory.Exists(pwd))
            {
                throw new EndUserException("The package has already been removed.");
            }

            using (await LockFile.LockAsync(GetLockFile(userId, projectName)))
            {
                try
                {
                    Process proc = StartProcess($"\"{_yarnPath}\" remove {packageName}", pwd);
                    await WaitForExitAsync(proc);

                    await TransformFilesAsync(pwd);

                    string output = await proc.StandardOutput.ReadToEndAsync();
                }
                catch (Exception e)
                {
                    throw new EndUserException("Internal error occured durning the uninstall of \"{packageName}\".", e);
                }
            }
        }
示例#3
0
        public void AsyncLock_ThrowsIfNotInitialized()
        {
            string   lockFilePath  = Path.Combine(PathHelper.TestLockPath, "uninitialized.lock");
            LockFile uninitialized = new LockFile(lockFilePath, NullTracerFactory.Instance);

            Assert.Throws <InvalidOperationException>(() => uninitialized.LockAsync());
        }
示例#4
0
        async Task <UserFile> IUserFileManager.WriteAsync(string userId, string projectName, string filePath, Stream data, string contentType)
        {
            // Get the base files directory.
            string baseDir = GetBaseDir(userId, projectName);

            using (await LockFile.LockAsync(GetLockFile(userId, projectName)))
            {
                List <UserFile> fileMetaList = null;
                try
                {
                    fileMetaList = await GetUserFilesAsync(userId, projectName);
                }
                catch (Exception e)
                {
                    await AsyncIO.WriteAllTextAsync($"{baseDir}\\filemeta.json", "[]", Encoding.UTF8);

                    fileMetaList = new List <UserFile>();
                }

                if (fileMetaList.TryFind(x => x.VirtualPath == filePath, out var _))
                {
                    return(await OverwriteFileAsync(baseDir, fileMetaList, filePath, data, contentType));
                }
                else
                {
                    return(await WriteNewFileAsync(baseDir, fileMetaList, filePath, data, contentType));
                }
            }
        }
示例#5
0
        async Task IUserFileManager.RenameAsync(string userId, string projectName, string oldFilePath, string newFilePath)
        {
            // Get the base files directory.
            string baseDir = GetBaseDir(userId, projectName);

            using (await LockFile.LockAsync(GetLockFile(userId, projectName)))
            {
                string fileMetaJson = null;
                try
                {
                    // Read the metadata file.
                    fileMetaJson = await AsyncIO.ReadAllTextAsync($"{baseDir}\\filemeta.json", Encoding.UTF8);
                }
                catch (FileNotFoundException e)
                {
                    ThrowNotFound(projectName, oldFilePath);
                }
                // Get the file meta dict.
                List <UserFile> fileMetaList = JsonConvert.DeserializeObject <List <UserFile> >(fileMetaJson);
                if (fileMetaList.TryFind(x => x.VirtualPath == oldFilePath, out var fileMeta))
                {
                    // Update the value in the list.
                    fileMetaList.Remove(fileMeta);
                    fileMetaList.Add(fileMeta.WithVirtualPath(newFilePath));
                }
                // Write the new file meta back to the file.
                await AsyncIO.WriteAllTextAsync($"{baseDir}\\filemeta.json", JsonConvert.SerializeObject(fileMetaList), Encoding.UTF8);
            }
        }
示例#6
0
        /**
         * Initializes the project if it has not already been initialized.
         */
        private async Task TryInitProjectAsync(string pwd, string userId, string projectName)
        {
            if (!Directory.Exists(pwd))
            {
                using (await LockFile.LockAsync(GetLockFile(userId, projectName)))
                {
                    await TryInstallToolsAsync();

                    Directory.CreateDirectory(pwd);
                    await AsyncIO.WriteAllTextAsync(pwd + "\\package.json", "{\"name\":\"creo-web-" + userId + "-" + projectName + "\",\"version\":\"1.0.0\",\"dependencies\":{}}", Encoding.UTF8);
                }
            }
        }
示例#7
0
        async Task <IReadOnlyList <UserFile> > IUserFileManager.GetFilesAsync(string userId, string projectName)
        {
            // Get the base files directory.
            string baseDir = GetBaseDir(userId, projectName);

            using (await LockFile.LockAsync(GetLockFile(userId, projectName)))
            {
                try
                {
                    return(await GetUserFilesAsync(userId, projectName));
                }
                catch (FileNotFoundException e)
                {
                    return(new List <UserFile>());
                }
            }
        }
示例#8
0
        async Task IUserFileManager.DeleteAsync(string userId, string projectName, string filePath)
        {
            // Get the base files directory.
            string baseDir = GetBaseDir(userId, projectName);

            using (await LockFile.LockAsync(GetLockFile(userId, projectName)))
            {
                var fileMetaList = await GetUserFilesAsync(userId, projectName);

                if (fileMetaList.TryFind(x => x.VirtualPath == filePath, out var fileMeta))
                {
                    File.Delete($"{baseDir}\\{fileMeta.LocalPath}");
                    // Remove from the list.
                    fileMetaList.Remove(fileMeta);
                }
                // Write the new file meta back to the file.
                await AsyncIO.WriteAllTextAsync($"{baseDir}\\filemeta.json", JsonConvert.SerializeObject(fileMetaList), Encoding.UTF8);
            }
        }
示例#9
0
        /**
         * Install a package asynchronously.
         */
        public async Task AddPackageAsync(string userId, string projectName, string packageName)
        {
            var pwd = $"{GetBaseDir(userId, projectName)}";

            await TryInitProjectAsync(pwd, userId, projectName);

            using (await LockFile.LockAsync(GetLockFile(userId, projectName)))
            {
                try
                {
                    Process proc = StartProcess($"{_yarnPath} add {packageName} --exact", pwd);
                    await WaitForExitAsync(proc);

                    await TransformFilesAsync(pwd);

                    string output = await proc.StandardOutput.ReadToEndAsync();
                }
                catch (Exception e)
                {
                    throw new EndUserException("Internal error occured durning the install of \"{packageName}\".", e);
                }
            }
        }
示例#10
0
        async Task <UserFileStream> IUserFileManager.ReadAsync(string userId, string projectName, string filePath)
        {
            // Get the base files directory.
            string baseDir = GetBaseDir(userId, projectName);

            using (await LockFile.LockAsync(GetLockFile(userId, projectName)))
            {
                var fileMetaList = await GetUserFilesAsync(userId, projectName);

                if (fileMetaList.TryFind(x => x.VirtualPath == filePath, out var fileMeta))
                {
                    return(new UserFileStream
                           (
                               stream: File.OpenRead($"{baseDir}\\{fileMeta.LocalPath}"),
                               metaData: fileMeta
                           ));
                }
                else
                {
                    ThrowNotFound(projectName, filePath);
                    throw null;
                }
            }
        }