Event on Create Project Package

Hi,

Does anyone know if there is an event after "Create Project Package" has been invoked?

Best regards,

Henk

Parents
  • I implemented a MonitorProjectPackageAction-class to monitor the Packages\Out-folder of the selected project. The moment a package is generated, an event will be raised. The class will wait until the file is fully written and then read the content of the sdlproj file in the sdlppx-file (which is a zip).
    I am sure that the code below can be optimized, but it works :-)

    CODE:
    ====
    using System;
    using System.Linq;
    using Sdl.Desktop.IntegrationApi;
    using Sdl.Desktop.IntegrationApi.Extensions;
    using Sdl.TranslationStudioAutomation.IntegrationApi;
    using System.IO;
    using System.IO.Compression;
    using System.Threading;
    using System.Text.RegularExpressions;

    namespace Dqf
    {
    [Action("MonitorProjectPackageAction", typeof(FilesController), Icon = "taus_icon")]
    public class MonitorProjectPackageAction : AbstractAction
    {
    static FileSystemWatcher _watcherOutFolder;

    public override void Initialize()
    {
    _watcherOutFolder = new FileSystemWatcher();
    _watcherOutFolder.Created += _watcherOutFolder_Created;

    var projectController = SdlTradosStudio.Application.GetController<ProjectsController>();

    projectController.SelectedProjectsChanged += (s, e) =>
    {
    _watcherOutFolder.EnableRaisingEvents = false;
    var pc = SdlTradosStudio.Application.GetController<ProjectsController>();
    if (pc.SelectedProjects.Count() != 1)
    {
    return;
    }

    _watcherOutFolder.Path = Path.GetDirectoryName(pc.SelectedProjects.First().FilePath);
    _watcherOutFolder.NotifyFilter = NotifyFilters.FileName;
    _watcherOutFolder.IncludeSubdirectories = true;
    _watcherOutFolder.Filter = "*.sdlppx";
    _watcherOutFolder.EnableRaisingEvents = true;
    };
    }

    private void _watcherOutFolder_Created(object sender, FileSystemEventArgs e)
    {
    //A new sdlppx was created. Detect if it was in Packages\out
    if (!Path.GetDirectoryName(e.FullPath).ToLower().EndsWith(@"packages\out"))
    {
    return;
    }

    //It was in Packages\Out!
    FileInfo fInfo = new FileInfo(e.FullPath);
    while (IsFileLocked(fInfo))
    {
    Thread.Sleep(500);
    }
    using (ZipArchive zip = ZipFile.Open(e.FullPath, ZipArchiveMode.Read))
    foreach (ZipArchiveEntry entry in zip.Entries)
    if (entry.Name.EndsWith(".sdlproj"))
    {
    string packageGUID = ReadPackageGUID(entry);
    if (packageGUID!=null)
    {
    //TODO@@@ Create child project, etc.

    return;
    }
    }
    }

    private static string ReadPackageGUID(ZipArchiveEntry entry)
    {
    string packageGUID = null;
    string tempFile = Path.GetTempFileName();
    File.Delete(tempFile);
    entry.ExtractToFile(tempFile);

    using (StreamReader sr = new StreamReader(tempFile))
    {
    sr.ReadLine(); //Skip first line with xml definition
    string pp = sr.ReadLine();

    Match m = Regex.Match(pp, @"PackageGuid=""(.+?)""");
    if (m.Groups.Count == 2)
    {
    //Done!
    packageGUID = m.Groups[1].Value;
    }
    else
    {
    //Unexpected error. The sdlppx file has not a PackageGuid
    }
    }
    File.Delete(tempFile);
    return packageGUID;
    }

    static bool IsFileLocked(FileInfo file)
    {
    FileStream stream = null;
    try
    {
    stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    }
    catch (IOException)
    {
    return true;
    }
    finally
    {
    if (stream != null)
    stream.Close();
    }
    return false;
    }


    /// <summary>
    /// The user has selected multiple files and wants to add DQF settings for those files
    /// </summary>
    protected override void Execute()
    {
    }
    }
    }

Reply
  • I implemented a MonitorProjectPackageAction-class to monitor the Packages\Out-folder of the selected project. The moment a package is generated, an event will be raised. The class will wait until the file is fully written and then read the content of the sdlproj file in the sdlppx-file (which is a zip).
    I am sure that the code below can be optimized, but it works :-)

    CODE:
    ====
    using System;
    using System.Linq;
    using Sdl.Desktop.IntegrationApi;
    using Sdl.Desktop.IntegrationApi.Extensions;
    using Sdl.TranslationStudioAutomation.IntegrationApi;
    using System.IO;
    using System.IO.Compression;
    using System.Threading;
    using System.Text.RegularExpressions;

    namespace Dqf
    {
    [Action("MonitorProjectPackageAction", typeof(FilesController), Icon = "taus_icon")]
    public class MonitorProjectPackageAction : AbstractAction
    {
    static FileSystemWatcher _watcherOutFolder;

    public override void Initialize()
    {
    _watcherOutFolder = new FileSystemWatcher();
    _watcherOutFolder.Created += _watcherOutFolder_Created;

    var projectController = SdlTradosStudio.Application.GetController<ProjectsController>();

    projectController.SelectedProjectsChanged += (s, e) =>
    {
    _watcherOutFolder.EnableRaisingEvents = false;
    var pc = SdlTradosStudio.Application.GetController<ProjectsController>();
    if (pc.SelectedProjects.Count() != 1)
    {
    return;
    }

    _watcherOutFolder.Path = Path.GetDirectoryName(pc.SelectedProjects.First().FilePath);
    _watcherOutFolder.NotifyFilter = NotifyFilters.FileName;
    _watcherOutFolder.IncludeSubdirectories = true;
    _watcherOutFolder.Filter = "*.sdlppx";
    _watcherOutFolder.EnableRaisingEvents = true;
    };
    }

    private void _watcherOutFolder_Created(object sender, FileSystemEventArgs e)
    {
    //A new sdlppx was created. Detect if it was in Packages\out
    if (!Path.GetDirectoryName(e.FullPath).ToLower().EndsWith(@"packages\out"))
    {
    return;
    }

    //It was in Packages\Out!
    FileInfo fInfo = new FileInfo(e.FullPath);
    while (IsFileLocked(fInfo))
    {
    Thread.Sleep(500);
    }
    using (ZipArchive zip = ZipFile.Open(e.FullPath, ZipArchiveMode.Read))
    foreach (ZipArchiveEntry entry in zip.Entries)
    if (entry.Name.EndsWith(".sdlproj"))
    {
    string packageGUID = ReadPackageGUID(entry);
    if (packageGUID!=null)
    {
    //TODO@@@ Create child project, etc.

    return;
    }
    }
    }

    private static string ReadPackageGUID(ZipArchiveEntry entry)
    {
    string packageGUID = null;
    string tempFile = Path.GetTempFileName();
    File.Delete(tempFile);
    entry.ExtractToFile(tempFile);

    using (StreamReader sr = new StreamReader(tempFile))
    {
    sr.ReadLine(); //Skip first line with xml definition
    string pp = sr.ReadLine();

    Match m = Regex.Match(pp, @"PackageGuid=""(.+?)""");
    if (m.Groups.Count == 2)
    {
    //Done!
    packageGUID = m.Groups[1].Value;
    }
    else
    {
    //Unexpected error. The sdlppx file has not a PackageGuid
    }
    }
    File.Delete(tempFile);
    return packageGUID;
    }

    static bool IsFileLocked(FileInfo file)
    {
    FileStream stream = null;
    try
    {
    stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    }
    catch (IOException)
    {
    return true;
    }
    finally
    {
    if (stream != null)
    stream.Close();
    }
    return false;
    }


    /// <summary>
    /// The user has selected multiple files and wants to add DQF settings for those files
    /// </summary>
    protected override void Execute()
    {
    }
    }
    }

Children
No Data