KiCad PCB EDA Suite
Loading...
Searching...
No Matches
jobs_output_archive.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2024 Mark Roszko <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22#include <wx/filename.h>
23#include <wx/fs_zip.h>
24#include <wx/wfstream.h>
25#include <wx/zipstrm.h>
26#include <gestfich.h>
27#include <common.h>
29#include <kiplatform/io.h>
30
36
37
39{
40 if( m_outputPath.IsEmpty() )
41 return false;
42
43 return true;
44}
45
46
47bool JOBS_OUTPUT_ARCHIVE::HandleOutputs( const wxString& baseTempPath,
48 PROJECT* aProject,
49 const std::vector<wxString>& aPathsWithOverwriteDisallowed,
50 const std::vector<JOB_OUTPUT>& aOutputsToHandle,
51 std::optional<wxString>& aResolvedOutputPath )
52{
53 bool success = true;
54 aResolvedOutputPath.reset();
55
56 wxString outputPath = m_outputPath;
57 outputPath.Replace( wxT( "\\" ), wxT( "/" ) );
58 outputPath = ExpandTextVars( outputPath, aProject );
59 outputPath = ExpandEnvVarSubstitutions( outputPath, aProject );
60
61 if( outputPath.StartsWith( "~" ) )
62 outputPath.Replace( "~", wxGetHomeDir(), false );
63
64 outputPath = EnsureFileExtension( outputPath, FILEEXT::ArchiveFileExtension );
65
66 // Ensure parent directory exists; matches the behavior of the folder output handler
67 // so that nested destination paths can be created on demand.
68 wxFileName outputFileName( outputPath );
69 wxString parentDir = outputFileName.GetPath();
70
71 if( !parentDir.IsEmpty() && !wxFileName::DirExists( parentDir ) )
72 {
73 if( !wxFileName::Mkdir( parentDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
74 {
75 aResolvedOutputPath.reset();
76 return false;
77 }
78 }
79
80 wxFFileOutputStream ostream( outputPath );
81
82 if( !ostream.IsOk() )
83 {
84 aResolvedOutputPath.reset();
85 return false;
86 }
87
88 // Use a large I/O buffer to improve compatibility with cloud-synced folders.
89 // See KIPLATFORM::IO::CLOUD_SYNC_BUFFER_SIZE comment for details.
90 if( FILE* fp = ostream.GetFile()->fp() )
91 setvbuf( fp, nullptr, _IOFBF, KIPLATFORM::IO::CLOUD_SYNC_BUFFER_SIZE );
92
93 wxZipOutputStream zipstream( ostream, -1, wxConvUTF8 );
94 wxString errors;
95
96 if( !AddDirectoryToZip( zipstream, baseTempPath, errors ) )
97 success = false;
98
99 if( !zipstream.Close() )
100 success = false;
101
102 if( success )
103 aResolvedOutputPath = outputPath;
104 else
105 aResolvedOutputPath.reset();
106
107 return success;
108}
109
110
111void JOBS_OUTPUT_ARCHIVE::FromJson( const nlohmann::json& j )
112{
113 m_outputPath = j.value( "output_path", "" );
115}
116
117
118void JOBS_OUTPUT_ARCHIVE::ToJson( nlohmann::json& j ) const
119{
120 j["output_path"] = m_outputPath;
121 j["format"] = "zip";
122}
123
124
126{
127 return _( "Archive" );
128}
bool OutputPrecheck() override
Checks if the output process can proceed before doing anything else This can include user prompts.
void FromJson(const nlohmann::json &j) override
void ToJson(nlohmann::json &j) const override
wxString GetDefaultDescription() const override
bool HandleOutputs(const wxString &aBaseTempPath, PROJECT *aProject, const std::vector< wxString > &aPathsWithOverwriteDisallowed, const std::vector< JOB_OUTPUT > &aOutputsToHandle, std::optional< wxString > &aResolvedOutputPath) override
Container for project specific data.
Definition project.h:66
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition common.cpp:708
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
Definition common.cpp:779
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
Definition common.cpp:63
The common library.
#define _(s)
bool AddDirectoryToZip(wxZipOutputStream &aZip, const wxString &aSourceDir, wxString &aErrors, const wxString &aParentDir)
Add a directory and its contents to a zip file.
Definition gestfich.cpp:632
static const std::string ArchiveFileExtension
static constexpr size_t CLOUD_SYNC_BUFFER_SIZE
Buffer size for file I/O operations on cloud-synced folders.
Definition io.h:77
Definition of file extensions used in Kicad.