How to get paths of newly added files in watchmode the webpack plugin? - webpack-5

I need to write a custom webpack plugin which will process newly added svg files in the src code in watchMode or recompile mode. But in my plugin compiler.modifiedFiles does not have the path of newly added files. It only contains the path till the folder where file is created. When I modify existing file it contains the filepath. In compiler, I found only modifiedFiles and removedFiles apis.
I am using webpack5.
Please let me know how can I get the path of newly added File in recompile scenario.
Here is sample code I am using
compiler.hooks.thisCompilation.tap( PLUGIN_NAME, ( compilation ) => {
compilation.hooks.finishModules.tapAsync( PLUGIN_NAME,
async( modules, callback ) => {
const modifiedSVGFiles = this.getModifiedSVGFiles( compilation.compiler );
this.getModifiedSVGFiles function
getModifiedSVGFiles( compiler ) {
const watchMode = compiler.watchMode;
let modifiedSVGFiles = {};
if( watchMode ) {
const modifiedFiles = compiler.modifiedFiles;
modifiedFiles && modifiedFiles.forEach( ( key, value ) => {
if( value.endsWith( '.svg' ) ) {
const key = `image/${basename( value )}`;
if( !modifiedSVGFiles[ key ] ) {
modifiedSVGFiles[ key ] = value;
}
}
} );
}
return modifiedSVGFiles;
}
Thanks
Prasad

Related

GetSymbolInfo().Symbol returning null on AttributeSyntax

I have a custom attribute I'm using to test a Roslyn code analyzer I'm writing:
[AttributeUsage( validOn: AttributeTargets.Class | AttributeTargets.Interface, Inherited = false, AllowMultiple = true )]
public class DummyAttribute : Attribute
{
public DummyAttribute( string arg1, Type arg2 )
{
}
public int TestField;
}
which decorates another test class:
[Dummy( "", typeof( string ) )]
[Dummy( "test", typeof( int ) )]
[Dummy( "test", typeof( int ) )]
public class J4JLogger<TCalling> : IJ4JLogger<TCalling>
{
But when I call GetSymbolInfo() on it with the semantic model it's defined in:
model.GetSymbolInfo( attrNode ).Symbol
the value of Symbol is null.
What's odd is that the GetSymbolInfo() call works perfectly well for attribute classes defined in the Net Core library (e.g., AttributeUsage).
Both Dummy and J4JLogger are defined in the same project. I create my compilation unit by parsing the files individually (e.g., J4JLogger is parsed and analyzed separately from Dummy) so when I'm parsing J4JLogger there is no reference to the assembly containing both J4JLogger and Dummy.
Could the problem be that model doesn't actually contain the Dummy class I think it does? Is there a way to check what's in the semantic model? Do I have to include a reference to the assembly whose source file I'm analyzing in the semantic model?
Corrected Parsing Logic
My original parsing logic parsed each file into a syntax tree independent of all its sister source files. The correct way to parse source files -- at least when they depend on each other -- is something like this:
protected virtual (CompilationUnitSyntax root, SemanticModel model) ParseMultiple( string primaryPath, params string[] auxPaths )
{
if( !IsValid )
return (null, null);
CSharpCompilation compilation;
SyntaxTree primaryTree;
var auxFiles = auxPaths == null || auxPaths.Length == 0
? new List<string>()
: auxPaths.Distinct().Where( p => !p.Equals( primaryPath, StringComparison.OrdinalIgnoreCase ) );
try
{
var auxTrees = new List<SyntaxTree>();
primaryTree = CSharpSyntaxTree.ParseText( File.ReadAllText( primaryPath ) );
auxTrees.Add( primaryTree );
foreach( var auxFile in auxFiles )
{
var auxTree = CSharpSyntaxTree.ParseText( File.ReadAllText( auxFile ) );
auxTrees.Add( auxTree );
}
compilation = CSharpCompilation.Create( ProjectDocument.AssemblyName )
.AddReferences( GetReferences().ToArray() )
.AddSyntaxTrees( auxTrees );
}
catch( Exception e )
{
Logger.Error<string>( "Configuration failed, exception message was {0}", e.Message );
return (null, null);
}
return (primaryTree.GetCompilationUnitRoot(), compilation.GetSemanticModel( primaryTree ));
}
A minor gotcha is that AddSyntaxTrees() does not appear to be incremental; you need to add all the relevant syntax trees in one call to AddSyntaxTrees().
Turns out the problem was that you have to include all the source files that reference each other (e.g., Dummy and J4JLogger in my case) in the compilation unit because otherwise the "internal" references (e.g., decorating J4JLogger with Dummy) won't resolve. I've annotated the question with how I rewrote my parsing logic.

How to pass project number to manifest.json

I am building a web push WordPress plugin and I want to pass project number from form input field to manifest.json file
which is included in index.php as
<link rel="manifest" href="/manifest.json">
Disclaimer: I'm the author of this plugin.
Instead of building your own from scratch, you could contribute to the already existing https://github.com/mozilla/wp-web-push.
If you want to build your own, you can check the source of that plugin out to see how we have implemented it.
We've built a class to handle it: https://github.com/marco-c/wp-web-app-manifest-generator.
You cannot pass any param to the manifest.json. You must genarate it as a static file when the form is submitted.
Here's the code that we have used for the Pushpad plugin:
if (file_exists ( ABSPATH . 'manifest.json' )) {
$oldManifestJson = file_get_contents ( ABSPATH . 'manifest.json' );
} else {
$oldManifestJson = '{}';
}
$data = json_decode ( $oldManifestJson, true );
$data ['gcm_sender_id'] = $settings ['gcm_sender_id'];
$data ['gcm_user_visible_only'] = true;
$newManifestJson = json_encode ( $data );
if ( is_writable ( ABSPATH . 'manifest.json' ) || !file_exists ( ABSPATH . 'manifest.json' ) && is_writable ( ABSPATH ) ) {
file_put_contents ( ABSPATH . 'manifest.json', $newManifestJson );
} else {
// display an error
}

RichTextBox SaveFile()

How can I pass a string as a path into here
void SaveLogFile()
{
logTxt->SaveFile(String::Concat
(System::Environment::GetFolderPath
(System::Environment::SpecialFolder::Personal),
"\\Testdoc.rtf"), RichTextBoxStreamType::RichNoOleObjs);
}
I can't figure out how to set a non SpecialFolder
From MSDN:
void SaveMyFile()
{
// Create a SaveFileDialog to request a path and file name to save to.
SaveFileDialog^ saveFile1 = gcnew SaveFileDialog;
// Initialize the SaveFileDialog to specify the RTF extention for the file.
saveFile1->DefaultExt = "*.rtf";
saveFile1->Filter = "RTF Files|*.rtf";
// Determine whether the user selected a file name from the saveFileDialog.
if ( saveFile1->ShowDialog() == System::Windows::Forms::DialogResult::OK &&
saveFile1->FileName->Length > 0 )
{
// Save the contents of the RichTextBox into the file.
richTextBox1->SaveFile( saveFile1->FileName );
}
}
See how the System::String^ is created here. Do it this way, too...

Getting only the folder path by browse button

I am getting a complete path of a file by browsing through the folder and selecting a file after i click browse button.
But i want to get path upto the folder only by browsing.
I did the following for the file path
Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->InitialDirectory = "c:\\";
//openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 2;
openFileDialog1->RestoreDirectory = true;
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
{
String^ p1 = openFileDialog1->FileName;
MessageBox ::Show (p1);
myStream->Close();
}
For getting folder paths rather than file names, use FolderBrowserDialog

Search a file in a directory

I work in a project and I need to know if a file is unique in a directory or not.
So how can I find if a file exists in a directory or not?
I have the file name without extension and the path of the directory.
There's no ready-made function for this I think, but you can use something like this:
static bool fileExists( const char *path )
{
const DWORD attr = ::GetFileAttributesA( path );
return attr != INVALID_FILE_ATTRIBUTES &&
( ( attr & FILE_ATTRIBUTE_ARCHIVE ) || ( attr & FILE_ATTRIBUTE_NORMAL ) );
}
This verifies that it's a "normal" file. You might want to add/remove flag checks if you want to deal with hidden files, too.
I prefer to do this on a C++ way, but you mentioned a Visual-C++ tag, so there's a way to do it on Visual-C++.NET:
using <mscorlib.dll>
using namespace System;
using namespace System::IO;
bool search(String folderPath, String fileName) {
String* files[] = Directory::GetFiles(folderPath, fileName+".*"); //search the file with the name fileName with any extension (remember, * is a wildcard)
if(files->getLength() > 0)
return true; //there are one or more files with this name in this folder
else
return false; //there arent any file with this name in this folder
}